我有以下JSON响应,需要解析为一个类。由于它没有任何root,我不知道如何解析它。
[{
"Id":18,
"Name":"Albume One",
"SubTitle":null,
"Description":null,
"Date":"3/7/2014 8:00 PM",
"City":null,
"State":null,
"Production":13
},
{"Id":200,
我使用以下代码,但不知道如何遍历结果集的每个字段:
JSONParser parser = new JSONParser();
JSONArray msg = (JSONArray)parser.parse(buff.readLine());
for(int i=0;i<msg.size();i++)
{
System.err.println(msg.get(0).toString());
}
{"Id":18,"Name":"Albume One","SubTitle":null,"Description":null,"Date":"3/7/2014 8:00 PM","City":null,"State":null,"Production":13}
{"Id":200, .....
是否有任何库可以自动解析JSON而不是使用循环,例如用于XML的Jackson库。
答案 0 :(得分:1)
使用以下
JSONParser parser=new JSONParser();
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=parser.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();
JSONObject obj2=(JSONObject)array.get(1);
System.out.println("======field \"1\"==========");
System.out.println(obj2.get("1"));
s="{}";
obj=parser.parse(s);
System.out.println(obj);
s="[5,]";
obj=parser.parse(s);
System.out.println(obj);
s="[5,,2]";
obj=parser.parse(s);
System.out.println(obj);
答案 1 :(得分:0)
你不需要根它。只需将其视为JSON数组并解析它。你将获得一组JSON对象。迭代它并获得你想要的价值......
尝试类似:
JSONParser parser = new JSONParser();
JSONArray msg = (JSONArray)parser.parse(buff.readLine());
for(int i=0;i<msg.length();i++)
{
JSONObject jsonobj= msg.getJSONObject(i);
sysout(jsonobj.get("KEY"));
}
答案 2 :(得分:-1)
您可以使用gson api来实现此目的。以下是示例代码:
public class Bean {
private int Id;
private String Name;
private String SubTitle;
private String Description;
private String Date;
private String City;
private String State;
private int Production;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSubTitle() {
return SubTitle;
}
public void setSubTitle(String subTitle) {
SubTitle = subTitle;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public int getProduction() {
return Production;
}
public void setProduction(int production) {
Production = production;
}
}
进行测试:
@Test
private void test() {
Gson g = new Gson();
String jsonString = "[{ \"Id\":18, \"Name\":\"Albume One\", \"SubTitle\":null, \"Description\":null, \"Date\":\"3/7/2014 8:00 PM\", \"City\":null, \"State\":null, \"Production\":13 }]";
Type token = new TypeToken<List<Bean>>(){}.getType();
List<Bean> list = g.fromJson(jsonString, token);
for (Bean bean : list) {
System.out.println(bean.getName());
}
}
答案 3 :(得分:-1)
取决于您使用的工具包......
JSONArray array = parser.parse(jsonString);
for (JSONObject object : array) {
int id = object.getInt("Id");
String name = object.getString("Name");
....
}