GSON `fromJson` can't parse JSON without quotes!
See original GitHub issueFor example:
{
name : Google Company,
age:19
}
That’s OK following codes, I can get correct value:
JsonObject jsonObject = new JsonParser().parse(jsonSt).getAsJsonObject();
String name = jsonObject.get("name").getAsString();
int age = jsonObject.get("age").getAsInt();
but it can’t parse into a bean class using fromJson
will cause Exception like this:
java.lang.RuntimeException:
com.google.gson.JsonSyntaxException:
com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 15 path $.name
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:10
Top Results From Across the Web
Convert json string without quotes into a map - Stack Overflow
To answer your question: There is no safe way of getting your JSON - which is not JSON by the way, because it's...
Read more >how can one strip quotes from the resulting JSON strings?
In this method, return the string in the way you want, which will be no quotes, and whatever else you wish to do...
Read more >How to read a JSON string without field name quotes in Jackson
We often have to read JSON values where the field names are not quoted, i.e., when enclosed with double ( "" ) or...
Read more >How to convert JSON String to Java Object? Gson ... - Java67
It is very simple, you just need to use the fromJson() method of Gson class and you are done. This is the simplest...
Read more >SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
You cannot use single-quotes around properties, like 'foo'. JSON.parse("{'foo': 1}"); ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is correct behavior. Your example is not valid JSON. See https://www.json.org/
One follow-up question, still: why does
JsonParser.parse(...)
accept content that does not enclose Object keys in double-quotes? I have seen test code that seems to rely on this behavior, but Javadocs:https://static.javadoc.io/com.google.code.gson/gson/2.8.5/com/google/gson/JsonParser.html#parse-java.io.Reader-
do not really mention anything about more lenient handling. So it would seem like this SHOULD indicate exception, too. But apparently it does not. Would that part be considered a bug, or perhaps missing documentation if more lenient handling is intentional?