Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

java - Reading a Json Array in android

I'm trying to read a JSON array. Here is my code.

        JSONArray jArray = new JSONArray(jsonString);

        System.out.println("*****JARRAY*****"+jArray.length());
        for(int i=0;i<jArray.length();i++){


                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag","_id"+json_data.getInt("account")+
                        ", mall_name"+json_data.getString("name")+
                        ", location"+json_data.getString("number")+
                        ", telephone"+json_data.getString("url")+
                        ",----"+json_data.getString("balance")+
                        ",----"+json_data.getString("credit")+
                        ",----"+json_data.getString("displayName")
                );

        }

And my sample JSON files syntax is as follows,

{
    "list": [
        {
            "account": 1,
            "name": "card",
            "number": "xxxxx xxxx xxxx 2002",
            "url": "http://www.google.com",
            "balance": 1.0,
            "credit": 1.0,
            "displayName": "hsbc bank" 
        },
        {
            "account": 2,
            "name": "card2",
            "number": "xxxxx xxxx xxxx 3003",
            "url": "http://www.google.com",
            "balance": 2.0,
            "credit": 2.0,
            "displayName": "nsb bank" 
        } 
    ],
    "count": 2
}

It has a curly brace in front. When i try to execute this code block it gives an error saying

A JSONArray text must start with '[' at character 1 of....

Has anyone encountered a problem like this? Any help would be greatly appreciated. Please show me a sample code block if can. Thanks in advance.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A JSON Object starts with a { and ends with a } while a JSON Array starts with a [ and ends with a ].

In your case, change your code to have a JSONObject instead.

JSONObject json = new JSONObject(jsonString);
JSONArray jArray = json.getJSONArray("list");

System.out.println("*****JARRAY*****" + jArray.length());

for(int i=0; i<jArray.length(); i++){
    JSONObject json_data = jArray.getJSONObject(i);

    Log.i("log_tag", "_id" + json_data.getInt("account") +
        ", mall_name" + json_data.getString("name") +
        ", location" + json_data.getString("number") +
        ", telephone" + json_data.getString("url") +
        ",----" + json_data.getString("balance") +
        ",----" + json_data.getString("credit") +
        ",----" + json_data.getString("displayName")
    );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...