1

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?

hamed
8,05517 gold badges66 silver badges119 bronze badges
asked Apr 4, 2015 at 10:28
1
  • First understand the difference between a JSON Object and a JSON Array. Commented Apr 4, 2015 at 10:41

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.