执行JSON解析时出错。?

时间:2013-05-16 04:18:58

标签: android json

我可以从url.But获取字符串响应当我尝试json解析该响应时,它会显示一些错误,如下所示。任何人都可以明白这一点。这是我的Api.java文件。

错误:

05-16 04:16:19.314: W/System.err(1582): org.json.JSONException: Value [] at needAttachment of type org.json.JSONArray cannot be converted to JSONObject
05-16 04:16:19.314: W/System.err(1582):     at org.json.JSON.typeMismatch(JSON.java:100)
05-16 04:16:19.323: W/System.err(1582):     at org.json.JSONObject.getJSONObject(JSONObject.java:573)
05-16 04:16:19.323: W/System.err(1582):     at com.example.twitterjson.TwitterApi.getconnect(TwitterApi.java:59)
05-16 04:16:19.323: W/System.err(1582):     at com.example.twitterjson.TwitterApi.doInBackground(TwitterApi.java:31)

JsonApi.java

package com.example.twitterjson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;

public class TwitterApi extends AsyncTask<Void, Integer, Void> implements
        TwitterArraylist {
    MainActivity activity;
    JSONArray jsonarray;

    public TwitterApi(MainActivity activity) {
        this.activity = activity;
        arraylist.clear();

    }

    protected void onPreExecute() {

        super.onPreExecute();
    }

    protected void onPostExecute(Void result) {
        activity.setup();
        super.onPostExecute(result);
    }

    protected Void doInBackground(Void... param) {
        getconnect();
        return null;

    }

    public void getconnect() {
        String url = "Some url";
        InternetManager manager = new InternetManager(url);
        String contentresponse = manager.Urlreq();
        if (contentresponse != null) {
            System.out.println("content_jsonresponse= " + contentresponse);
            try {

                JSONObject mainobject = new JSONObject(contentresponse);
                JSONArray needarray = mainobject.getJSONArray("needs");
                for (int i = 0; i < needarray.length(); i++) {
                    JSONObject needarrayobject = needarray.getJSONObject(i);
                    JSONObject needobject = needarrayobject
                            .getJSONObject("need");
                    TwitterModel t = new TwitterModel();
                    t.setId(needobject.getString("id"));
                    t.setText(needobject.getString("description"));
                    t.setTime(needobject.getString("created"));
                    JSONObject needattach = needarrayobject
                            .getJSONObject("needAttachment");
                    t.setName(needattach.getString("title"));
                    arraylist.add(t);

                }

                System.out.println("CONTENT_MODEL.size()" + arraylist.size());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

我的杰森:

{
  "needs": [
    {
      "need": {
        "id": "197",
        "title": "A new roof1",
        "description": "Im hoping this shit works.",
        "location_id": "1",
        "status": "1",
        "created": "2013-04-12 06:04:04"
      },
      "needAttachment": [

      ]
    },
    {
      "need": {
        "id": "196",
        "title": "Test10",
        "description": "This is the test 10 one",
        "location_id": "1",
        "status": "1",
        "created": "2013-04-03 03:47:24"
      },
      "needAttachment": [

      ]
    },
    {
      "need": {
        "id": "194",
        "title": "Draft test",
        "description": "Test test",
        "location_id": "1",
        "status": "1",
        "created": "2013-03-29 06:10:36"
      },
      "needAttachment": {
        "id": "118",
        "title": null,
        "file": "\/files\/uploads\/need\/f6ad4d9a473458da7a9aabfec4fdef1b"
      }
    },
    {
      "need": {
        "id": "192",
        "title": "kitchen sink",
        "description": "My kitchen sink is clogged",
        "location_id": "1",
        "status": "1",
        "created": "2013-03-28 11:15:51"
      },
      "needAttachment": {
        "id": "117",
        "title": null,
        "file": "\/files\/uploads\/need\/4a49d9eda8aab623e4dcb6fafd124115"
      }
    },
    {
      "need": {
        "id": "191",
        "title": "test5",
        "description": "This is test5",
        "location_id": "1",
        "status": "1",
        "created": "2013-03-28 07:23:57"
      },
      "needAttachment": {
        "id": "120",
        "title": null,
        "file": "\/files\/uploads\/need\/31eba0e0570c6d16df7df8706c1dbcb3"
      }
    },
    {
      "need": {
        "id": "190",
        "title": "tree removal",
        "description": "tree removal",
        "location_id": "1",
        "status": "1",
        "created": "2013-03-26 18:25:52"
      },
      "needAttachment": {
        "id": "110",
        "title": null,
        "file": "\/files\/uploads\/need\/890724880928cda88d3d665aaad2239d"
      }
    },
    {
      "need": {
        "id": "189",
        "title": "Roof 3",
        "description": "Roof",
        "location_id": "1",
        "status": "1",
        "created": "2013-03-25 15:08:33"
      },
      "needAttachment": {
        "id": "99",
        "title": null,
        "file": "\/files\/uploads\/need\/3fff1990c035edba6fb2a728f247ef7b"
      }
    },
    {
      "need": {
        "id": "188",
        "title": "test3",
        "description": "Test3 done here",
        "location_id": "1",
        "status": "1",
        "created": "2013-03-25 04:04:45"
      },
      "needAttachment": {
        "id": "102",
        "title": null,
        "file": "\/files\/uploads\/need\/f3e3a58076441b73000c098e5c5884b7"
      }
    },
    {
      "need": {
        "id": "187",
        "title": "MyBook1",
        "description": "Test for MyBook1",
        "location_id": "1",
        "status": "1",
        "created": "2013-03-24 23:13:56"
      },
      "needAttachment": {
        "id": "104",
        "title": null,
        "file": "\/files\/uploads\/need\/61d3daf4f8bc67706d86577303bacd75"
      }
    },
    {
      "need": {
        "id": "186",
        "title": "spring cleaning",
        "description": " Time for spring clean. testing the edit feature. ",
        "location_id": "1",
        "status": "1",
        "created": "2013-03-24 18:47:09"
      },
      "needAttachment": {
        "id": "107",
        "title": null,
        "file": "\/files\/uploads\/need\/0914f9b5fa843c0ee8bebea109c9444d"
      }
    }
  ]
}

2 个答案:

答案 0 :(得分:0)

如错误所述,您尝试将 needAttachments 的值解析为JSONObject

JSONObject needattach = needarrayobject.getJSONObject("needAttachment");

虽然是JSONArray。所以你实际上需要解析它:

JSONArray needattach = needarrayobject.getJSONArray("needAttachment");

答案 1 :(得分:0)

在你的Json needAttachment中是一个对象,但是当它为空时它是一个空数组。 因此,如果您可以控制它,请将空数组更改为Json数据中的空对象{},或者捕获异常并将其解析为其他ansers中提到的数组。