For 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
This is correct behavior.
Your example is not valid JSON.
See https://www.json.org/
@danorton2 Hi, I know it's a invalid JSON. My puzzle is why I can parse it into a JsonObject and get value correctly, but can't parse it into a bean. :)
In my opinion, it should do parse into a bean because it can get value from a JsonObject.
@SwayChen Just to be clear, you want fromJson try to parse invalid Json into a bean? Why?
@rafritts @danorton2
Don't make misunderstanding :)
What make me puzzle is why JsonObject can parse that invalid JSON at the same time the valid JSON can't be parse into a bean.
The point is "Why JsonObject can recognize that invalid JSON", I know JSON must be with quotes mark after I meet program. Now JSON without quotes can be parsed. -_-
How to reproduce?
String invalidJson = ""
+ "{\n"
+ " name : Google Company,\n"
+ " age:19\n"
+ "}";
new JsonParser().parse(invalidJson);
This will (correctly) throw com.google.gson.stream.MalformedJsonException...
I also have like issue but my case seem like have space, please see https://github.com/thinkerou/karate-grpc/issues/3 thanks!
I also have like issue but my case seem like have space, please see thinkerou/karate-grpc#3 thanks!
The author have marked as a bug.
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?
One follow-up question, still: why does
JsonParser.parse(...)accept content that does not enclose Object keys in double-quotes?
See #41, #372 and #1208 on current lenient situation.
Hola, he citado recientemente una duda en: https://stackoverflow.com/questions/43412261/make-gson-accept-single-objects-where-it-expects-arrays/58943838#58943838
Con respecto al mismo problema pero ya encontr茅 el origen del problema, no s茅 c贸mo solucionarlo.
Gracias
Most helpful comment
This is correct behavior.
Your example is not valid JSON.
See https://www.json.org/