Android/Java 中的 JSON 数组迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3408985/
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
JSON Array iteration in Android/Java
提问by Kevin Bradshaw
I am building an android app that needs to download and synchronise with an online database, I am sending my query from the app to a php page which returns the relevant rows from a database in JSON format.
我正在构建一个需要下载并与在线数据库同步的 android 应用程序,我将我的查询从应用程序发送到一个 php 页面,该页面以 JSON 格式从数据库中返回相关行。
can someone please tell me the best way to iterate through a JSON array?
有人可以告诉我遍历 JSON 数组的最佳方法吗?
I receive an array of objects:
我收到一组对象:
[{json object},{json object},{json object}]
[{json object},{json object},{json object}]
What is the simplest piece of code I could use to access the JSONObjects in the array?
我可以用来访问数组中的 JSONObjects 的最简单的一段代码是什么?
EDIT: now that I think of it the method I used to iterate the loop was:
编辑:现在我想到了我用来迭代循环的方法是:
for (String row: json){
id = row.getInt("id");
name = row.getString("name");
password = row.getString("password");
}
So I guess I had was somehow able to turn the returned Json into and iterable array. Any Ideas how I could achieve this?
所以我想我以某种方式能够将返回的 Json 变成可迭代的数组。我有什么想法可以实现这一目标吗?
I apologise for my vaguness but I had this working from an example I found on the web and have since been unable to find it.
我为我的含糊不清而道歉,但我从网上找到的一个例子中得到了这个,但此后一直无法找到它。
采纳答案by Nathan Schwermann
I have done it two different ways,
我做了两种不同的方法,
1.) make a Map
1.) 制作地图
HashMap<String, String> applicationSettings = new HashMap<String,String>();
for(int i=0; i<settings.length(); i++){
String value = settings.getJSONObject(i).getString("value");
String name = settings.getJSONObject(i).getString("name");
applicationSettings.put(name, value);
}
2.) make a JSONArray of names
2.) 制作一个名字的 JSONArray
JSONArray names = json.names();
JSONArray values = json.toJSONArray(names);
for(int i=0; i<values.length(); i++){
if (names.getString(i).equals("description")){
setDescription(values.getString(i));
}
else if (names.getString(i).equals("expiryDate")){
String dateString = values.getString(i);
setExpiryDate(stringToDateHelper(dateString));
}
else if (names.getString(i).equals("id")){
setId(values.getLong(i));
}
else if (names.getString(i).equals("offerCode")){
setOfferCode(values.getString(i));
}
else if (names.getString(i).equals("startDate")){
String dateString = values.getString(i);
setStartDate(stringToDateHelper(dateString));
}
else if (names.getString(i).equals("title")){
setTitle(values.getString(i));
}
}
回答by Paul Burke
On Arrays, look for:
在数组上,查找:
JSONArray menuitemArray = popupObject.getJSONArray("menuitem");
JSONArray menuitemArray = popupObject.getJSONArray("menuitem");
回答by vipw
I think this code is short and clear:
我认为这段代码简短而清晰:
int id;
String name;
JSONArray array = new JSONArray(string_of_json_array);
for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
id = row.getInt("id");
name = row.getString("name");
}
Is that what you were looking for?
这就是你要找的吗?
回答by Thunderforge
If you're using the JSON.org Java implementation, which is open source, you can just make JSONArray implement the Iterable
interface and add the following method to the class:
如果您使用的是开源的JSON.org Java 实现,您只需让 JSONArray 实现Iterable
接口并将以下方法添加到类中:
@Override
public Iterator iterator() {
return this.myArrayList.iterator();
}
This will make all instances of JSONArray iterable, meaning that the for (Object foo : bar)
syntax will now work with it (note that foo has to be an Object, because JSONArrays do not have a declared type). All this works because the JSONArray class is backed by a simple ArrayList, which is already iterable. I imagine that other open source implementations would be just as easy to change.
这将使 JSONArray 的所有实例都可迭代,这意味着for (Object foo : bar)
语法现在可以使用它(注意 foo 必须是一个对象,因为 JSONArrays 没有声明的类型)。所有这些都有效,因为 JSONArray 类由一个简单的 ArrayList 支持,它已经是可迭代的。我想其他开源实现也很容易改变。
回答by 18446744073709551615
While iterating over a JSON array (org.json.JSONArray, built into Android), watch out for nullobjects; for example, you may get "null"
instead of a nullstring.
在迭代 JSON 数组(org.json.JSONArray,内置于 Android)时,注意空对象;例如,您可能会得到"null"
而不是空字符串。
A check may look like:
支票可能如下所示:
s[i] = array.isNull(i) ? null : array.getString(i);
回答by Dream
Unfortunately , JSONArray
doesn'tsupport foreach
statements, like:
不幸的是,JSONArray
不支持foreach
语句,例如:
for(JSONObject someObj : someJsonArray) {
// do something about someObj
....
....
}
回答by Andriya
You are using the same Cast object for every entry. On each iteration you just changed the same object instead creating a new one.
您为每个条目使用相同的 Cast 对象。在每次迭代中,您只是更改了同一个对象,而不是创建一个新对象。
This code should fix it:
此代码应该修复它:
JSONArray jCastArr = jObj.getJSONArray("abridged_cast");
ArrayList<Cast> castList= new ArrayList<Cast>();
for (int i=0; i < jCastArr.length(); i++) {
Cast person = new Cast(); // create a new object here
JSONObject jpersonObj = jCastArr.getJSONObject(i);
person.castId = (String) jpersonObj.getString("id");
person.castFullName = (String) jpersonObj.getString("name");
castList.add(person);
}
details.castList = castList;
回答by Graham S.
When I tried @vipw's suggestion, I was faced with this exception:
The method getJSONObject(int) is undefined for the type JSONArray
当我尝试@vipw 的建议时,我遇到了这个异常:
The method getJSONObject(int) is undefined for the type JSONArray
This worked for me instead:
这对我有用:
int myJsonArraySize = myJsonArray.size();
for (int i = 0; i < myJsonArraySize; i++) {
JSONObject myJsonObject = (JSONObject) myJsonArray.get(i);
// Do whatever you have to do to myJsonObject...
}