Am having a problem reading/parsing the following JSON string in Java.
Code:
try{
json = new JSONObject(result);
//json now looks like this :-
// {'header': '[{"doc_no": "DN00001","stage":"P"}]','section':'[{"upper":100,"lower":1]'}
if (json != null){
// this line is throwing an exception!!
JSONObject header = new JSONObject("header");
}catch(JSONException e){
// Error Message
}
I’ve also tried this:
JSONArray header = json.getJSONArray("header");
but still throwing some exception.
What am I missing?
Here dude take this code. Fix your JSON string if you wanted to get JSONObject out of it
So what’s wrong with you? Couple of things:
Your JSON string is all illegal. Thanks to parsers to bear with you. It should be
It wont solve your problem alone. Since you wanted to get
JSONObjectbut you provided aJSONArray(why did you do that?). So remove those square brackets.Still not happy. You see you are trying to create a new
JSONObjectby doing (obviously)new JSONObject("header")using a string taht is not a JSON. 9And expecting it not to throw error? How cruel.) Plus you wanted togetnotset. So usejson.getXXX("header")where XXX can beString,JSONObjectorJSONArrayand many more.