[英]Parsing JSON array and objects Android
我剛開始學習,需要你的幫助。 我對以下輸出中的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的數組項
如何正確解析數據? 對於圖像對象可能需要運行 ......你可以建議哪些代碼將數據傳輸到變量? 感謝您的時間...!
要將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 > ();
}
使用以下方法解析上述數據
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();
}
}
快樂的編碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.