In my project i have a complex json response. I want to read it by GSon.
JSON : {'FoodMenuRS':{'Results':[{'Items':{'Item':[{'#Id':'24'},{'#Id':'24'}]}}, {'Items':{'Item':{'#Id':'24'}}}]}}
It contains a JSONArray with first "Item" and JSONObject with second one. Hence its call results in error,
failed to deserialize json object {"#Id":"24"} given the type java.util.List<com.servlet.action.ItemInfo> and java.lang.IllegalStateException: This is not a JSON Array.
Please help how i should handle this scenario. Thanks.
The string you are showing is a JSONObject not a JSONArray. So, in this case you first of all have to get the JSONObject and perform further decoding on that JSONObject.
JSONObject - {}
JSONArray - []
And indeed JSONObject or JSONArray should be encoded using Double-quotes(")
Your JSON is valid, but not for the doble quotes (") because JSON supports simple quotes (') and no quotes in the key name. See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Colle
However this JSON have key names that begin with #. For JSON strings this character is valid at the beginning of the name (see right column http://www.json.org/) but for Java this names are illegal (see Naming section http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html). Specifically, names started with # are annotations and you can't use annotations tags to declare variables, fields, methods, etc.
This is not a valid JSON object. Strings in JSON are always encapsulated in double quotes ("). Contact the producer of that JSON and tell him to use a correct encoder.
Related
I have a JSON which is converted from xml. (XML to JSON)
But the converted JSON file has some datatype issue, which is not matched with the expected JSON schema
For ex: expected is accountId: "123" (in Json schema type is mentioned as String)
But actual result is accountId: 123 (it received as a number)
While converting XML to JSON (StAXON parse) makes like this, yes offcourse I didn't feed any datatype related parameters while converting XML to JSON.
From here I have two questions:
Is there any way to resolve my issue while xml to json convertion itself
Or else from my Json, I need to validate with Json schema and change the datatype accordingly.
Guide me to do this in better way.
Feel free to edit the question as my text is not understandable/misleading.
Note: I am using Spring Integration!!!
I am parsing a json file that contains values that are comma separated.
Such as {"values":[{"key":"some, value", "key2":"somevalue"}]}
When parsing this using the org.json library it is using the commas in the first key as a delimiter so it is parsing them as separate objects.
JSONTokener tokener = new JSONTokener(new FileReader(someJsonFile));
JSONObject a = new JSONObject(tokener);
//I believe the problem is occuring here
JSONArray values = (JSONArray) instruments.get("values");
I want the values that include commas to be treated as one value. Instead of being parsed into 'some' and 'value', i want it to be 'some, value' as indicated in the file.
You should create a POJO class that is equivalent to the JSon String format. Then you can use jackson to convert the JSon string to pojo objects. It will not have the mentioned issue.
Reference : https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
In the example
If the json object has a missing value for 'age',
String source = "{
name : John;
age : ;
score: 100
}"
here the json might be in wrong format, I am not sure about this. And I want to ignore those keys which don't have values.
JSONObject object = objectMapper.readValue(source, JSONObject.class);
Error getting:
com.fasterxml.jackson.databind.RuntimeJsonMappingException: Instantiation of [simple type, class org.json.JSONObject] value failed: Missing value at [some line number] (through reference chain: com.bial.rs.model)
Note : JSONObject is from org.json package
If you want to avoid NullPointerException you better make use of optString() method to extract the values from JSON
If you are fetching the data from JSON at any time, you might have null data for a particular Key value, at that time instead of implementing Null conditions, better make use of this optimized method optString("age") it will return null and does not throw JSONException
I'm unable to parse escaped json string through JSONObject, while trying to parse it, I always get an Unterminated Object error. But when I manually remove all of the escape characters from the json string, JSONObject parses it successfully.
But the problem is that for complex or nested json strings, I don't want to remove escape characters, because for nested structures, unescaping the characters results in an invalid json string.
This is the json string after removing escapes characters and I get unterminated object error due to invalid json when parsing through JSONObject
{"result":"unknown","message":"{"firebase":"unknown"}","sender":"unknown"}
This is the unescaped json string
{"result":"unknown","message":"{\"firebase\":\"unknown\"}","sender":"unknown"}
If you look at the json you are constructing it in wrong format.
In case of escaped json
{
"result":"unknown",
"message":"{"firebase":"unknown"}",
"sender":"unknown"
}
the json object "message" is having value "{" and firebase is causing exception.
In the the escaped json "message":{"firebase":"unknown"} will form valid json.
i am not familiar with json but as far as i know this should be a valid JSON string
{text:" Klan: Fury Elo: 9004 ",color:gold,extra:[{text:">>[ INFO ]<<",clickEvent:{action:run_command,value:"/klan info Fury"},hoverEvent:{action:show_text,value:"Zobrazis informaci o klanu."},color:gray,bold:true},{text:" "},{text:">>[ JOIN ]<<",color:green,bold:true,clickEvent:{action:run_command,value:"/klan join Fury"},hoverEvent:{action:show_text,value:"Podas zadost o vstoupeni do klanu."}}]}
But i am getting this error
Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 3
Can somebody explain where is an error?
It is not. Keys must be strings.
Whenever in doubt check out JSONLint - a great resource for format validation.
Color and extra are not wrapped around double quote. For more information, read https://en.m.wikipedia.org/wiki/JSON