无法将类型为java.lang.String的值数组转换为JSONObject

时间:2016-03-23 19:16:31

标签: java android

我已经启动了android编程,并且最近遇到了几个问题。 我从数据库中获取数据并使用POSTMAN,我可以在下面看到JSON中的结果,但在解析它时,它给了我错误:

{
  "result": [
    {
      "Date": "18-3-2016",
      "Events": "Local Holiday"
    },
    {
      "Date": "23-3-2016",
      "Events": "Monthly Fees"
    },
    {
      "Date": "15-4-2016",
      "Events": "Monthly Fees"
    },
    {
      "Date": "23-4-2016",
      "Events": "Annual Day"
    },
    {
      "Date": "30-4-2016",
      "Events": "session end"
    },
    {
      "Date": "9-4-2016",
      "Events": "Parent Teacher Meeting"
    }
  ]
}

我正在使用代码显示,但我收到错误,我的代码是:

private void showJSON(String response) {

    String child_name = "";
    String Date ="";
    String address="";
    //String vc = "";
    try {
        final String TAG = events.class.getSimpleName();
        Log.d(TAG, "showJSON: \n"+response);
        JSONObject jsonObject = new JSONObject(response);

        //Get the instance of JSONArray that contains JSONObjects
       // JSONArray jsonArray = jsonRootObject.optJSONArray(config_events.JSON_ARRAY);
        JSONArray result = jsonObject.getJSONArray(config_events.JSON_ARRAY);
        //Iterate the jsonArray and print the info of JSONObjects
        for (int i = 0; i < result.length(); i++) {
            //JSONObject jsonObject = jsonArray.getJSONObject(i);
            //JSONObject jsonObject = new JSONObject(response);

            Log.d(TAG, "showJSON: Event list size: "+result.length());
            JSONObject collegeData = result.getJSONObject(0);
            child_name = collegeData.getString(config_events.KEY_NAME);
            address = collegeData.getString(config_events.KEY_ADDRESS);
            //vc = collegeData.getString(config.KEY_VC);


            textViewResult.setText("EVENTS:\t" + address);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

错误:

Value Array of type java.lang.String cannot be converted to JSONObject
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:158)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:171)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.weavearound.schools.weavearound.events.showJSON(events.java:105)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.weavearound.schools.weavearound.events.access$100(events.java:25)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.weavearound.schools.weavearound.events$1.onResponse(events.java:82)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.weavearound.schools.weavearound.events$1.onResponse(events.java:78)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at android.os.Handler.handleCallback(Handler.java:605)
03-23 19:05:35.982 2128-2128/com.weavearound.schools.weavearound W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at android.os.Looper.loop(Looper.java:137)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4517)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
03-23 19:05:35.992 2128-2128/com.weavearound.schools.weavearound W/System.err:     at dalvik.system.NativeStart.main(Native Method)

回复代码:

String url = config_events.DATA_URL;

        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                loading.dismiss();
                showJSON(response);
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(events.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest

2 个答案:

答案 0 :(得分:0)

您正在发出字符串请求。改为创建一个JsonObjectRequest。然后你会收到像这样的json响应 -

showJSON(JsonObject response)

然后从中取出结果 -

JSONArray result = response.getJSONArray("result");

答案 1 :(得分:0)

你能改变那些......确定

            JSONArray result = jsonObject.getJSONArray("result");

            for (int i = 0; i < result.length(); i++) {

                JSONObject collegeData = result.getJSONObject(i);
                child_name = collegeData.getString("Date");
                address = collegeData.getString("Events");