WISHY
WISHY

Reputation: 11999

Converting json string to JSONArray in java?

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?

Upvotes: 1

Views: 187

Answers (1)

hamed
hamed

Reputation: 8043

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);

Upvotes: 1

Related Questions