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?

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

Comments

Your Answer

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.