2015-04-04 68 views
1

我有这个JSON字符串从第三方API在java中将json字符串转换为JSONArray?

{"msg":"xff","uuid":"44037b67-3629-4325-83e5-7a00cb78dfdf"} 

返回当我尝试下面的代码

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

我得到这个下面例外解析它

org.json.JSONException: Value {"msg":"xff","uuid":"44037b67-3629-4325-83e5-7a00cb78dfdf"} of type org.json.JSONObject cannot be converted to JSONArray 

什么我可以用其他方式解析它吗?

+0

先了解JSON对象和JSON阵列之间的差异。 – 2015-04-04 10:41:13

回答

1

您可以使用JSONObject代替:

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

thanx效果不错 – WISHY 2015-04-04 10:35:40

+0

不客气! – hamed 2015-04-04 10:37:06