如何在android中解析这个json

时间:2012-03-22 17:46:12

标签: android json

我需要解析这个json并获得itemvotecounts和firstrankvotescounts。

[
    [
        {
            "itm_id": "22",
            "itm_nam": "movie1",
            "user_id": "8",
            "overall": [
                {
                    "itm_id": "23",
                    "itm_nam": "movie2",
                    "firstrankvotecounts": "27",
                    "overallrank": 1,
                    "overalltotalitems": 16,
                    "overallmaxrank": "16"
                }
            ],
            "itemoverallrank": "2",
            "itemvotecounts": "23",
            "particularcategory": [
                {
                    "itm_id": "23",
                    "itm_nam": "movie2",
                    "firstrankvotecounts": "27",
                    "particularcategoryrank": 1,
                    "particularcategorytotalitems": 12,
                    "categorymaxrank": "12"
                }
            ],
            "itemparticularcategoryrank": "2"
        }
    ],
    [
        {
            "itm_id": "42",
            "itm_nam": "How to train your dragon",
            "user_id": "8",
            "overall": [
                {
                    "itm_id": "23",
                    "itm_nam": "movie2",
                    "firstrankvotecounts": "27",
                    "overallrank": 1,
                    "overalltotalitems": 16,
                    "overallmaxrank": "16"
                }
            ],
            "itemoverallrank": "9",
            "itemvotecounts": "5",
            "particularcategory": [
                {
                    "itm_id": "23",
                    "itm_nam": "movie2",
                    "firstrankvotecounts": "27",
                    "particularcategoryrank": 1,
                    "particularcategorytotalitems": 12,
                    "categorymaxrank": "12"
                }
            ],
            "itemparticularcategoryrank": "8"
        }
    ]
]

我使用了以下代码。

try {  
JSONArray jsonArray = new JSONArray("the json");
Log.v("Number of entries ",Integer.toString(jsonArray.length()));
                for (int i = 1; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String itm_vote = jsonObject.getString("itemvotecounts");
                    Log.v("item vote count ",itm_vote);     
                }} catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

并收到以下错误。

03-22 23:05:01.067: W/System.err(389): org.json.JSONException: JSONArray[1] is not a JSONObject.
03-22 23:05:01.077: W/System.err(389):  at org.json.JSONArray.getJSONObject(JSONArray.java:268)
03-22 23:05:01.077: W/System.err(389):  at com.http.post.HttpPostCheckActivity$responseCheck.doInBackground(HttpPostCheckActivity.java:90)
03-22 23:05:01.077: W/System.err(389):  at com.http.post.HttpPostCheckActivity$responseCheck.doInBackground(HttpPostCheckActivity.java:1)
03-22 23:05:01.087: W/System.err(389):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-22 23:05:01.087: W/System.err(389):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
03-22 23:05:01.087: W/System.err(389):  at java.util.concurrent.FutureTask.run(FutureTask.java:122)
03-22 23:05:01.097: W/System.err(389):  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
03-22 23:05:01.097: W/System.err(389):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
03-22 23:05:01.097: W/System.err(389):  at java.lang.Thread.run(Thread.java:1060)

1 个答案:

答案 0 :(得分:0)

json字符串中的

... json对象位于json数组的第二级。

JSONArray jsonArray = new JSONArray("your_json_string");

for (int i = 0; i < jsonArray.length(); i++) {
    JSONArray jsonArrayInner = jsonArray.getJSONArray[i];
    JSONObject jsonObject = jsonArrayInner.getJSONObject(0);
    String itm_vote = jsonObject.getString("itemvotecounts");
    Log.v("item vote count ",itm_vote);     
}

根据您的json,这将是合适的代码..