我有这个JSON字符串,我需要获取每个docmanId
和每个dz
,因此,我可以遍历它们并使用它们。
我尝试使用gson
库来做到这一点,但我似乎并没有弄清楚。
JSON数组:
[{"docmanId":1,"dz":"CR"},
{"docmanId":1,"dz":"EU"},
{"docmanId":1,"dz":"JD"},
{"docmanId":1,"dz":"LT"},
{"docmanId":10,"dz":"CR"},
{"docmanId":10,"dz":"EU"},
{"docmanId":10,"dz":"LT"},
{"docmanId":100,"dz":"CR"},
{"docmanId":100,"dz":"EU"},
{"docmanId":100,"dz":"JD"},
{"docmanId":100,"dz":"LT"},
{"docmanId":1000,"dz":"CR"},
{"docmanId":1000,"dz":"EU"},
{"docmanId":1000,"dz":"JD"},
{"docmanId":1000,"dz":"LT"},
{"docmanId":10000,"dz":"ES"},
{"docmanId":10000,"dz":"EU"},
{"docmanId":10000,"dz":"JD"},
{"docmanId":100000,"dz":"CR"},
{"docmanId":100000,"dz":"EU"},
{"docmanId":100000,"dz":"JD"},
{"docmanId":100000,"dz":"LT"},
{"docmanId":100001,"dz":"CR"},
{"docmanId":100001,"dz":"EU"},
{"docmanId":100001,"dz":"LT"},
{"docmanId":100002,"dz":"CR"},
{"docmanId":100002,"dz":"EU"},
{"docmanId":100002,"dz":"JD"},
{"docmanId":100003,"dz":"CR"},
{"docmanId":100003,"dz":"EU"},
{"docmanId":100003,"dz":"JD"},
{"docmanId":100003,"dz":"LT"},
{"docmanId":100004,"dz":"CR"},
{"docmanId":100004,"dz":"EU"},
{"docmanId":100004,"dz":"JD"},
{"docmanId":100005,"dz":"CR"},
{"docmanId":100005,"dz":"EU"},
{"docmanId":100005,"dz":"JD"},
{"docmanId":100005,"dz":"LT"},
{"docmanId":100006,"dz":"CR"},
{"docmanId":100006,"dz":"EU"},
{"docmanId":100006,"dz":"JD"},
{"docmanId":100006,"dz":"LT"},
{"docmanId":100007,"dz":"CR"},
{"docmanId":100007,"dz":"EU"},
{"docmanId":100007,"dz":"JD"}]
答案 0 :(得分:2)
使用org.json
JSONArray jSONArray = new JSONArray("your input array");
int length = jSONArray.length();
for (int i = 0; i < length; i++) {
JSONObject jSONObject= jSONArray.getJSONObject(i);
System.out.println(jSONObject.get("docmanId"));
System.out.println(jSONObject.get("dz"));
}
答案 1 :(得分:1)
与jackson
String json = "[{\"docmanId\":1,\"dz\":\"CR\"},\n" +
"{\"docmanId\":1,\"dz\":\"EU\"},\n" +
"{\"docmanId\":1,\"dz\":\"JD\"},\n" +
"{\"docmanId\":1,\"dz\":\"LT\"},\n" +
"{\"docmanId\":10,\"dz\":\"CR\"},\n" +
"{\"docmanId\":10,\"dz\":\"EU\"},\n" +
"{\"docmanId\":10,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100,\"dz\":\"LT\"},\n" +
"{\"docmanId\":1000,\"dz\":\"CR\"},\n" +
"{\"docmanId\":1000,\"dz\":\"EU\"},\n" +
"{\"docmanId\":1000,\"dz\":\"JD\"},\n" +
"{\"docmanId\":1000,\"dz\":\"LT\"},\n" +
"{\"docmanId\":10000,\"dz\":\"ES\"},\n" +
"{\"docmanId\":10000,\"dz\":\"EU\"},\n" +
"{\"docmanId\":10000,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100000,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100000,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100000,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100000,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100001,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100001,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100001,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100002,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100002,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100002,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100003,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100003,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100003,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100003,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100004,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100004,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100004,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100005,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100005,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100005,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100005,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100006,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100006,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100006,\"dz\":\"JD\"},\n" +
"{\"docmanId\":100006,\"dz\":\"LT\"},\n" +
"{\"docmanId\":100007,\"dz\":\"CR\"},\n" +
"{\"docmanId\":100007,\"dz\":\"EU\"},\n" +
"{\"docmanId\":100007,\"dz\":\"JD\"}]";
ObjectMapper objectMapper = new ObjectMapper();
DocmanList docmanList = objectMapper.readValue(json, DocmanList.class);
//logic below
}
public class Docman {
private long docmanId;
private String dz;
public long getDocmanId() {
return docmanId;
}
public void setDocmanId(long docmanId) {
this.docmanId = docmanId;
}
public String getDz() {
return dz;
}
public void setDz(String dz) {
this.dz = dz;
}
}
public class DocmanList extends ArrayList<Docman> {
}
答案 2 :(得分:0)
您可以通过生成一个在list的java对象中将其转换的类来做到这一点。
首先生成一个类
----------------------------------- com.example.Example.java ----- ------------------------------
package com.example;
import java.io.Serializable;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Example implements Serializable
{
@SerializedName("docmanId")
@Expose
private long docmanId;
@SerializedName("dz")
@Expose
private String dz;
private final static long serialVersionUID = 3195470113916852896L;
/**
* No args constructor for use in serialization
*
*/
public Example() {
}
/**
*
* @param docmanId
* @param dz
*/
public Example(long docmanId, String dz) {
super();
this.docmanId = docmanId;
this.dz = dz;
}
public long getDocmanId() {
return docmanId;
}
public void setDocmanId(long docmanId) {
this.docmanId = docmanId;
}
public Example withDocmanId(long docmanId) {
this.docmanId = docmanId;
return this;
}
public String getDz() {
return dz;
}
public void setDz(String dz) {
this.dz = dz;
}
public Example withDz(String dz) {
this.dz = dz;
return this;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("docmanId", docmanId).append("dz", dz).toString();
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(docmanId).append(dz).toHashCode();
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Example) == false) {
return false;
}
Example rhs = ((Example) other);
return new EqualsBuilder().append(docmanId, rhs.docmanId).append(dz, rhs.dz).isEquals();
}
}
Now Tell it to parse a List (of Welcome) instead. Since List is generic you will typically use a **TypeReference**
List<Welcome> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Welcome>>(){});
使用http://www.jsonschema2pojo.org/将json转换为java类。
答案 3 :(得分:0)
您可以使用JsonPath对其进行解析:
static String json = "...";
public static void main(String[] args) {
String pageName = JsonPath.read(json, "$.pageInfo.pageName");
System.out.println(pageName);
Integer posts = JsonPath.read(json, "$.posts.length()");
for(int i=0; i < posts; i++) {
String post_id = JsonPath.read(json, "$.posts[" + i + "].post_id");
System.out.println(post_id);
}
}