从JSONArray中检索数组

时间:2017-10-29 18:55:57

标签: android json

我有以下JSON字符串:

{
    "error":false,
    "message":"modules retrieved successfully",
    "subjectsEnrolled":
    {
        "studentNumber":"PT2014-1282",
        "courseId":1,
        "moduleNames":["Internet Programming and E-commerce","Operating Systems","Java and Distributed Systems"]
    }
}

我希望检索moduleNames中的值。

尝试检索值的当前方法是:

@Override
protected void onPostExecute(String s) {

    super.onPostExecute(s);

    try {

        JSONObject object = new JSONObject(s);
        JSONArray array = object.getJSONArray("moduleNames");

        if (!object.getBoolean("error")) {
            Toast.makeText(getActivity(), object.getString("message"), Toast.LENGTH_SHORT).show();

        } else {
        }
    } catch (JSONException e) {
        e.printStackTrace();
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
}

遵循以下追踪:

10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err: org.json.JSONException: No value for moduleNames
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at org.json.JSONObject.get(JSONObject.java:355)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:549)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at com.example.vhuhwavho.mylms.ModulesFragment$PerformNetworkRequest.onPostExecute(ModulesFragment.java:166)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at com.example.vhuhwavho.mylms.ModulesFragment$PerformNetworkRequest.onPostExecute(ModulesFragment.java:137)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:632)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at android.os.Looper.loop(Looper.java:146)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5487)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
10-29 20:48:50.741 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
10-29 20:48:50.746 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
10-29 20:48:50.746 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
10-29 20:48:50.746 14484-14484/com.example.vhuhwavho.mylms W/System.err:     at dalvik.system.NativeStart.main(Native Method)
10-29 20:48:50.746 14484-14484/com.example.vhuhwavho.mylms E/JSON Parser: Error parsing data org.json.JSONException: No value for moduleNames

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

JsonArray位于JsonObject内部,因此您必须先获取内部对象 subjectEnrolled ,然后按如下方式获取数组:

    JSONObject root_object = new JSONObject(s);
    JSONObject object = root_object.getJSONObject("subjectsEnrolled");
    JSONArray array = object.getJSONArray("moduleNames");

答案 1 :(得分:0)

试试这个

    JSONObject object = new JSONOBject(s);
    String error = object.getString("error");
    String message = object.getString("message");

    JSONObject obj2 = object.getJSONObject("subjectsEnrolled");
    String studentnumber = obj2.getString("studentNumber");
    String courseid = obj2.getString("courseId");


    JSONArray jarr1 = obj2.getJSONArray("moduleNames");
    Log.d("checkjsonarrayresponsehere==", ""+jarr1);

答案 2 :(得分:0)

检查一下,如上所述,你必须先得到内部对象 然后需要一个类似于此的步骤来读取数组中的每个值。

   for(JsonValue value : arrObj){
      System.out.println(value.toString());
   }

http://www.java2novice.com/java-json/javax.json/read-json-array/