Java 在android中读取Json数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4244879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:57:21  来源:igfitidea点击:

Reading a Json Array in android

javaandroidjson

提问by nala4ever

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

我正在尝试读取 JSON 数组。这是我的代码。

        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,

我的示例 JSON 文件语法如下,

{
    "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....

JSONArray 文本必须以 '[' 开头的字符 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.

有没有人遇到过这样的问题?任何帮助将不胜感激。如果可以,请给我看一个示例代码块。提前致谢。

采纳答案by Buhake Sindi

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

JSON 对象以 a 开头并以 a{结尾,}而 JSON 数组以 a 开头并以 a[结尾]

In your case, change your code to have a JSONObjectinstead.

在您的情况下,请更改您的代码以JSONObject改为使用。

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

回答by Matt Colliss

You first need to create a JSONObject to get the Array from, something like this should work:

您首先需要创建一个 JSONObject 来从中获取数组,这样的事情应该可以工作:

JSONObject jsonObject = new JSONObject(jsonString);

JSONArray jArray = jsonObject.getJSONArray("list");

回答by ??????? ????????

String result = js.getString("Result");

String result = js.getString("Result");

                    JSONArray js2 = new JSONArray(result);
                    for (int i = 0; i < js2.length(); i++) {
                        JSONObject js3 = js2.getJSONObject(i);
                        categoriescity.add(js3.getString("Title"));
                    }