如何解析多个对象JSON
?请帮帮我。
我的JSON,
{
"result": [
[{
"date": "21.12.2016",
"day": "\u0421\u0435\u0440\u0435\u0434\u0430",
"time": "08:00-09:20",
"subject": "\u0424\u0456\u0437\u0438\u0447\u043d\u0430 \u0440\u0435\u0430\u0431\u0456\u043b\u0456\u0442\u0430\u0446\u0456\u044f (\u041f\u0440\u0421)",
"room": "\u0430\u0443\u0434.309\/4 \u043a\u043e\u0440\u043f\u0443\u0441 \u0432\u0438\u043a\u043b\u0430\u0434\u0430\u0447 \u041a\u0430\u0440\u043f\u0435\u043d\u043a\u043e \u042e.\u041c."
}, {
"date": "21.12.2016",
"day": "\u0421\u0435\u0440\u0435\u0434\u0430",
"time": "09:30-10:50",
"subject": "\u041f\u043e\u043b\u0456\u0442\u043e\u043b\u043e\u0433\u0456\u044f (\u041f\u0440\u0421)",
"room": "\u0430\u0443\u0434.408\/4 \u043a\u043e\u0440\u043f\u0443\u0441 \u0434\u043e\u0446\u0435\u043d\u0442 \u0412\u0435\u0434\u043c\u0435\u0434\u0454\u0432 \u041c.\u041c."
}, {
"date": "21.12.2016",
"day": "\u0421\u0435\u0440\u0435\u0434\u0430",
"time": "11:00-12:20",
"subject": "(\u043f\u0456\u0434\u0433\u0440. 2) \u0413\u0456\u043c\u043d\u0430\u0441\u0442\u0438\u043a\u0430 \u0442\u0430 \u043c\u0435\u0442\u043e\u0434\u0438\u043a\u0430 \u0457\u0457 \u0432\u0438\u043a\u043b\u0430 (\u041f\u0440\u0421)",
"room": "\u0430\u0443\u0434.\u0413\u0456\u043c\u043d\u0430\u0442\u0438\u0447\u043d\u0438\u0439 \u0437\u0430\u043b\/\u0421\u043f\u043e\u0440\u0442\u043a\u043e\u043c\u043f\u043b\u0435\u043a\u0441 \u0441\u0442. \u0432\u0438\u043a\u043b\u0430\u0434\u0430\u0447 \u0411\u0430\u043b\u0430\u0448\u043e\u0432 \u0414.\u0406."
}]
]
}
答案 0 :(得分:2)
你应该这样做
JsonObject obj = new JsonObject(responseString);
JsonArray result = obj.getJsonArray("result");
for(int i = 0 ; i < result.length() ; i++){
JsonArray dataArray = result.getJsonArray(i);
for(int j = 0 ; j < dataArray.length() ; j++){
String date = dataObj.optString("date");
...
}
}
答案 1 :(得分:1)
org.codehaus.jackson.map.ObjectMapper 使用此库进行映射。并使用以下方法将您的pojo转换为理想的类: 的 http://www.jsonschema2pojo.org/ 强> 有关详细信息: http://wiki.fasterxml.com/JacksonInFiveMinutes
喜欢:
package com.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"result"
})
public class Example {
@JsonProperty("result")
private List<List<Result>> result = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("result")
public List<List<Result>> getResult() {
return result;
}
@JsonProperty("result")
public void setResult(List<List<Result>> result) {
this.result = result;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
然后另一个java类将是
package com.example;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"date",
"day",
"time",
"subject",
"room"
})
public class Result {
@JsonProperty("date")
private String date;
@JsonProperty("day")
private String day;
@JsonProperty("time")
private String time;
@JsonProperty("subject")
private String subject;
@JsonProperty("room")
private String room;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("date")
public String getDate() {
return date;
}
@JsonProperty("date")
public void setDate(String date) {
this.date = date;
}
@JsonProperty("day")
public String getDay() {
return day;
}
@JsonProperty("day")
public void setDay(String day) {
this.day = day;
}
@JsonProperty("time")
public String getTime() {
return time;
}
@JsonProperty("time")
public void setTime(String time) {
this.time = time;
}
@JsonProperty("subject")
public String getSubject() {
return subject;
}
@JsonProperty("subject")
public void setSubject(String subject) {
this.subject = subject;
}
@JsonProperty("room")
public String getRoom() {
return room;
}
@JsonProperty("room")
public void setRoom(String room) {
this.room = room;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}