[英]Read an array of JSON in JAVA
I have a response like this: 我有这样的回应:
[ { "key": { "kind": "UserRecord", "id": 0, "name": "1" }, "firstName": "1", "lastName": "1", "homeLat": 0.0, "homeLon": 0.0, "workLat": 0.0, "workLon": 0.0, "currentLat": 10.0, "currentLon": 10.82, "timestamp": 1335735046606, "score": 0, "isStarred": false, "distance": 0.0 }, { "key": { "kind": "UserRecord", "id": 0, "name": "32423542324234324" }, "firstName": "Simone", "lastName": "Boscolo Berto", "homeLat": 0.0, "homeLon": 0.0, "workLat": 0.0, "workLon": 0.0, "currentLat": 55.786444, "currentLon": 12.515867, "timestamp": 1335884083696, "score": 0, "isStarred": false, "distance": 0.0 } ]
and a method that from each JSONObject give me my object 以及每个JSONObject给我我的对象的方法
private User getUserFromJson(JSONObject jsonUser)
How can I iterate in that list of JSONObjects? 如何在该JSONObjects列表中进行迭代? should I use JSONArrayobject?
我应该使用JSONArrayobject吗?
This could be easy with Gson or jackson 使用Gson或jackson可能很容易
Gson example 格森的例子
Gson gson = new Gson();
User[] users = gson.fromJson(<json string>, User[].class);
Jackson example 杰克逊的例子
ObjectMapper mapper = new ObjectMapper();
Collection<User> users =
mapper.readValue(<json string>, `new TypeReference<Collection<User>>() {});
Sample - 样品 -
package com.test;
import com.google.gson.Gson;
public class GSonExample {
public static void main(String[] args) {
String json = "{\"name\":\"Duke\",\"address\":\"Menlo Park\",\"dateOfBirth\":\"Feb 1, 2000 12:00:00 AM\"}";
Gson gson = new Gson();
User student = gson.fromJson(json, User.class);
System.out.println("student.getName() = " + student.getName());
System.out.println("student.getAddress() = " + student.getAddress());
System.out.println("student.getDateOfBirth() = " + student.getDateOfBirth());
}
}
public class User {
private String name;
private String address;
private String dateOfBirth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
classpath 类路径
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/gson-2.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Something like this: 像这样:
final String incomingJSON ;
final JSONArray objArray = new JSONArray(incomingJSON);
for(int i = 0; i < objArray.length(); i++) {
final JSONObject obj = objArray.getJSONObject(i);
final User user = getUserFromJson(obj);
}
Are you using the Gson api? 您在使用Gson API吗? With that, you can get an iterator from a JsonArray object.
这样,您就可以从JsonArray对象获得迭代器。
Iterator<JsonElement> iterator()
Returns an iterator to navigate the elemetns of the array.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.