简体   繁体   中英

Error occures while doing JSON parsing.?

I can get the string response from url.But When i am trying json parsing of that response it shows some errors like below.Can anyone give any idea to clear that.? Here is my Api.java file.

Error:

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

}

My Json :

{
  "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"
      }
    }
  ]
}

As the error describes, you are trying to parse the value of needAttachments as a JSONObject :

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

While it is a JSONArray . so you actually need to parse it as such:

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

In your Json needAttachment is an object, but when it is empty it is an empty array. So if you have control over it change the empty array to en empty object {} in your Json data or catch the exception and parse it to an array like mentioned in the other ansers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM