我必须关注服务器的响应:page:{"result":"ok","id_photo":36782710}
。我正试图通过这样做来获取result
:
String r=page.toString();
JSONArray jArray = new JSONArray(r);
JSONObject jsdata = r.getJSONObject(0);
String result = jsdata.getString("result");
System.out.println("Raspunsul de la server este:" +result);
但是我收到以下错误:
WARN/System.err(30452): org.json.JSONException: Value {"result":"ok","id_photo":36782710} of type org.json.JSONObject cannot be converted to JSONArray
at org.json.JSON.typeMismatch(JSON.java:107)
at org.json.JSONArray.<init>(JSONArray.java:91)
at org.json.JSONArray.<init>(JSONArray.java:103)
at com.Contest.DialogExampleActivity.executeMultiPartPost(DialogExampleActivity.java:131)
at com.Contest.DialogExampleActivity.btnSend_onClick(DialogExampleActivity.java:88)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at android.view.View$1.onClick(View.java:2157)
at android.view.View.performClick(View.java:2534)
at android.view.View$PerformClick.run(View.java:9210)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method
有人有任何想法吗?
答案 0 :(得分:3)
尝试将其实例化为JSONObject,而不是将解析不是数组而是对象的数据实例化。
JSONObject json = new JSONObject(response);
String result = json.getString("result");
答案 1 :(得分:2)
正如madsleejensen所说,它应该被视为JSONObject
而不是JSONArray
,并且对于将来的引用,在使用正确类型时的简单建议是:JSONObject
由{}
覆盖{1}}和JSONArray
涵盖[]
。
答案 2 :(得分:1)