I'm trying to parse a JSON string, but getting an error when trying to get a nested object:
JSONObject jsonObject = new JSONObject(jsonString);
System.out.println(jsonObject);
System.out.println(jsonObject.keySet());
System.out.println(jsonObject.getJSONObject("matches"));
Below is the output in console. As far as I can see, the JSON is valid as jsonObject is created without an error. But when I try to obtain "matches" it throws an error. I've compared my code with tutorials but I can't see what the issue out to be:
{"matches":[{"id":233028,"awayTeam":{...
[matches, count, filters, competition]
Error in client: JSONObject["matches"] is not a JSONObject.
Anything I'm doing wrong? Happy to provide any further info if needed.
matches is an array, not an object. Use getJSONArray:
System.out.println(jsonObject.getJSONArray("matches"));
(Or more usefully:
System.out.println(Arrays.deepToString(jsonObject.getJSONArray("matches")));
since System.out.println on an array doesn't really show useful information on its own.)
Related
I'm using this code in Java:
JSONObject jsonObject = new JSONObject(response.body().string());
and it will lead to null when I put strings like
[{...},{...}, ..., {...}]
where {...} is a valid JsonObject.
What should I do? I guess json must begin with { and end with } always. That's because it's null. What should I do? Is there a way to make JSON library deal with this automatically? I can't control the place from where I receive this string of 'json array'.
Because the string you've given isn't an object, but an array.
You'll have to read it like that (using Java EE's JSON libraries):
JsonArray array = jsonReader.readArray();
I am using version 2.4.0 of the com.jayway.jsonpath json-path library. I have attempting to parse an invalid document, but I done't get an exception thrown. The code is like
this.json= Configuration.defaultConfiguration().jsonProvider().parse(document);
The value of the document variable passed into the parse method is "notvalidjson". However the call returns fine assigning the string "notvalidjson" to the member json Object. Is this expected behaviour> I was expecting an InvalidJsonException to be thrown.
Thanks,
Paul
Managed to fix this rather than using JSONArray res = JsonPath.read(document, selector); I have used JSONArray res = JsonPath.parse(document).read(selector, JSONArray.class)
I am working on a spring MVC application. I am using org.json jar to deal with jsonObjects. I am trying to find if a JSONObject contains a JSON array. Can any one please help me in this:
Say i have a JSON Object:
{"Object":{
"array":["",""]
}
}
So i want to know if array exists in that json object as some times the JSONObject will be :
{"Object":{
"array11":["",""]
}
}
Please help me.
Per the javadocs for the org.json JSON library, JSONObject has an optJSONArray() method that does not throw an exception if the key you are targeting does not exist or the value at that key is not a JSON array. For a JSONObject "foo":
JSONArray array = foo.optJSONArray("array");
if (array != null) {
// do stuff with the array
}
You can use org.json (https://github.com/stleary/JSON-java) to do this. You can simply use this code to create the JSON Object:
JSONObject object = new JSONObject(String);
And then you can use the isNull(String) method to check if a value exists in the object. You can get the array using the getJSONArray(String) method, create a loop (such as a for loop) and loop through the array (which accepts ints instead of Strings).
i m trying to make app that can communicate with mysql (localhost)
Only problem is Eclipse is showing JSONParser Error
After trying to find solution for error i found this Tutorial jackson library tho http://jackson.codehaus.org/
i could not found any solution after 2 days digging into android code
JSONParser jParser = new JSONParser();
ERROR: Multiple markers at this line
- JSONParser cannot be resolved
to a type
- JSONParser cannot be resolved
to a type
and this line of code has same error showing
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
Please help me so i can continue my app
help will highly appreciate!
thanks
If you need to parse a JSON string, you do not need to use any JSONParser class. The JSONObject itself can be used for parsing JSON.
Pass the JSON string to a new JSON object as following:
JSONObject responseObject = new JSONObject(response); //response is the JSON string that you get as response
String name = responseObject.get("name");
Similarly you can get the values directly from the responseObject itself. No need to use any third party libraries. You can find a very good documentation here.
EDIT:
I prefer Android Asynchronous Http Client for making web requests.
im struggling with json again :(
Here is the original response:
{"xml-fragment":{"workItem":{"#id":"251","#version":"74"},"presentation":{"#formIdenitifier":"1.0.0.201310151421/openspaceGWTPull_DefaultChannel/.default/Npda/NpdaProcess/UserReconcile/UserReconcile.gwt.json","#type":"GWT_FORM","#version":"1.0.0.201310151421","#activityName":"UserReconcile"},"workTypeDetail":{"#typePiled":"false","#pilingLimit":"0","#uid":"WT__RIoPEDWTEeOr4-yR8gXd7g","#version":"1.0.0.201310151421"},"payloadModel":{"serializedPayload":"{items:[{\"$param\":\"BankReconInput\",\"mode\":\"IN\",\"$value\":[{\"bankAccountTx_pk\":\"55213\",\"amount\":\"10099\",\"reference\":\"ImAmReference\",\"date\":\"2013-10-15\",\"reconType\":\"?\",\"amxcaseref\":\"pvm:0a12iq\",\"$type\":\"coza.npda.bom.BankTransaction\"}]}]}","#payloadMode":"JSON"}}}
i want to for example get value of amount from the serializedPayload. The problem is that it is not a json object. If i try:
obj = new JSONObject(jsonResp).getJSONObject("xml-fragment").getJSONObject("payloadModel");
this returns to me serializedPayload as a string and #payloadMode as a string.
i tried:
obj = new JSONObject(jsonResp).getJSONObject("xml-fragment").getJSONObject("payloadModel").getJSONObject("serializedPayload");
its confirms that serializedPayload is not a json object.
I looked at this example: http://developer.android.com/reference/org/json/JSONTokener.html
But its data is not as complex as mine and i am struggling to find java examples of how to do this.
Please can anyone help.
You don't need an example, you need to look at the JSON and think for a second.
serializedPayload is not a JSON object to begin with, it's really a string that has another piece of json encoded inside, sort of like the russian nesting dolls (frankly, it's an abomination).
You need to take the string, and then parse it again, using another JSONObject, sort of:
String payload = data..getJSONObject("xml-fragment").getJSONObject("payloadModel").getString("serializedPayload");
JSONObject theRealData = new JSONObject(payload);