繁体   English   中英

解析JSON数组和Android对象

[英]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"
                }
            ]
        }
    ]
}

最后我有一个带有两个对象bodyimage的数组

如何正确解析数据? 对于图像对象可能需要运行 ......你可以建议哪些代码将数据传输到变量? 感谢您的时间...!

要将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM