阅读在Android的JSON数组 [英] Reading a Json Array in android
问题描述
我想读一个JSON数组。这是我的code。
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")
);
}
和我的示例JSON文件语法如下,
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
}
它在前面一个大括号。当我尝试执行此code座它给出了一个错误说
It has a curly brace in front. When i try to execute this code block it gives an error saying
一个JSONArray文本必须以'[' 在....性格1
A JSONArray text must start with '[' at character 1 of....
有没有人遇到过这样的问题吗?任何帮助将大大AP preciated。 请给我一个样品code座如果能。 先谢谢了。
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.
推荐答案
一个JSON对象开始于一个 {
并用结束}
而JSON数组以一个 [
和结尾的]
。
A JSON Object starts with a {
and ends with a }
while a JSON Array starts with a [
and ends with a ]
.
在你的情况,改变你的code有一个的JSONObject
代替。
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")
);
}
这篇关于阅读在Android的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!