我刚开始学习,需要你的帮助。 我对以下输出中的JSON解析有点困惑... 这是我的输出......
{
"item": [
{
"body": [
{
"item_name": "tralala",
"publish_date": "2015-10-08 ",
"price": null,
"code": null,
"contact_name": "somename",
"contact_email": "somemail",
"contact_number": "989899833",
"image_format": "jpg",
"img_url": "uploads/",
"state": "",
"city": "",
"area": ""
}
],
"image": [
{
"image_name": "2443"
},
{
"image_name": "2444"
},
{
"image_name": "2445"
},
{
"image_name": "2446"
},
{
"image_name": "2447"
},
{
"image_name": "2448"
},
{
"image_name": "2449"
},
{
"image_name": "2450"
},
{
"image_name": "2451"
},
{
"image_name": "2452"
},
{
"image_name": "2453"
},
{
"image_name": "2454"
}
]
}
]
}
最后我有一个数组项,其中有两个对象 body 和 image
如何正确解析数据? 对于图片对象,可能需要运行 ... 您可以建议将数据传输到变量中的代码是什么? 谢谢你的时间......!
答案 0 :(得分:0)
要将json数据传输到变量,您可以使用http://www.jsonschema2pojo.org/
这是你的结果:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Image {
@SerializedName("image_name")
public String imageName;
}
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Body {
@SerializedName("item_name")
public String itemName;
@SerializedName("publish_date")
public String publishDate;
@SerializedName("price")
public Object price;
@SerializedName("code")
public Object code;
@SerializedName("contact_name")
public String contactName;
@SerializedName("contact_email")
public String contactEmail;
@SerializedName("contact_number")
public String contactNumber;
@SerializedName("image_format")
public String imageFormat;
@SerializedName("img_url")
public String imgUrl;
@SerializedName("state")
public String state;
@SerializedName("city")
public String city;
@SerializedName("area")
public String area;
}
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("item")
public List < Item > item = new ArrayList < Item > ();
}
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Item {
@SerializedName("body")
public List < Body > body = new ArrayList < Body > ();
@SerializedName("image")
public List < Image > image = new ArrayList < Image > ();
}
答案 1 :(得分:0)
使用以下方法
解析上述数据 public void parseData() {
List<String> imglist=new ArrayList<String>();
JSONObject jobj = new JSONObject(response);
JSONArray jarr = null;
try {
jarr = jobj.getJSONArray("item");
JSONObject job1 = jarr.getJSONObject(0);
JSONArray body_arr = job1.getJSONArray("body");
JSONObject job2 = body_arr.getJSONObject(0);
String item_name = job2.getString("item_name");
//similarly other strings present in job2
JSONArray img_arr = job1.getJSONArray("image");
for (int i = 0; i < img_arr.length();i++)
{
JSONObject img_ob = jarr.getJSONObject(i);
String img_ame=img_ob.getString("image_name");
imglist.add(img_ame);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
快乐编码