I have this json string returned from a 3rd party api
{"msg":"xff","uuid":"44037b67-3629-4325-83e5-7a00cb78dfdf"}
When I try to parse it by the below code
JSONArray json = new JSONArray(message.toString());
JSONArray arr = json.getJSONArray(0);
String mess = arr.getJSONObject(0).getString("msg");
String uuid = arr.getJSONObject(0).getString("uuid");
System.out.println("message : "+mess);
System.out.println("uuid : "+uuid);
I get this below exception
org.json.JSONException: Value {"msg":"xff","uuid":"44037b67-3629-4325-83e5-7a00cb78dfdf"} of type org.json.JSONObject cannot be converted to JSONArray
What other way can I parse it?
-
First understand the difference between a JSON Object and a JSON Array.Buhake Sindi– Buhake Sindi2015年04月04日 10:41:13 +00:00Commented Apr 4, 2015 at 10:41
1 Answer 1
You can use JSONObject instead:
JSONObject obj = new JSONObject(message);
String mess = obj.getString("msg");
String uuid = obj.getString("uuid");
System.out.println("message : "+mess);
System.out.println("uuid : "+uuid);
answered Apr 4, 2015 at 10:32
hamed
8,05517 gold badges66 silver badges119 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-java