在Android中循环使用多个JSON对象

时间:2015-09-27 15:15:12

标签: java android json

我试图解析一个JSON对象,其中一部分看起来像这样:

{
  "status":{
  "rcode":200,
  "message":"OK"
},
"data":{
   "0":{
     "SubFranchiseID":"0",
     "OutletID":"607",
     "OutletName":"Spill ",
     "BrandID":"403",
     "Address":"J-349, JP Road, Opp. Apna Bazar, JP Rd, D.N.Nagar, Andheri West, Mumbai, Maharashtra, India",
     "NeighbourhoodID":"1",
     "CityID":"1",
     "Email":null,
     "Timings":"Everyday: 6 pm to 1:30 am.",
     "CityRank":null,
     "Latitude":"19.127473",
     "Longitude":"72.832545",
     "Pincode":null,
     "Landmark":null,
     "Streetname":null,
     "BrandName":"Spill ",
     "OutletURL":"https:\/\/plus.google.com\/111539701326240643109\/about?hl=en-US",
     "NumCoupons":1,
     "NeighbourhoodName":"Andheri West",
     "PhoneNumber":"+91 22 2642 5895",
     "CityName":"Mumbai",
     "Distance":8205.2235,
     "Categories":[
        {
           "OfflineCategoryID":"32",
           "Name":"Continental",
           "ParentCategoryID":"1",
           "CategoryType":"Cuisine"
        },
        {
           "OfflineCategoryID":"13",
           "Name":"Bar and Restaurant",
           "ParentCategoryID":"1",
           "CategoryType":"TypeOfRestaurant"
        },
        {
           "OfflineCategoryID":"17",
           "Name":"Italian",
           "ParentCategoryID":"1",
           "CategoryType":"Cuisine"
        },
        {
           "OfflineCategoryID":"1",
           "Name":"Restaurant",
           "ParentCategoryID":null,
           "CategoryType":""
        },
        {
           "OfflineCategoryID":"21",
           "Name":"North Indian",
           "ParentCategoryID":"1",
           "CategoryType":"Cuisine"
        }
     ],
     "LogoURL":"http:\/\/www.google.in\/sitespecific\/media\/generated\/offlineimages\/logo_403.jpg",
     "CoverURL":"http:\/\/www.google.in\/sitespecific\/media\/generated\/offlineimages\/cover_607.jpg"
  },
 "1 "{
      "SubFranchiseID":"1",
     "OutletID":"60",
     "OutletName":"Bill ",
       .
       .
       .
      }
}

每个对象都有近35个Json对象和嵌套的JsonArrays。我试图获取这样的数据:

  url = new URL(uri);
        URLConnection connection = url.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        reader.close();
        json =sb.toString();
        JSONObject sjson=json.getJSONObject("data");
        Log.d("sjson values-->",sjson+"--");
        **JSONObject ssjson=sjson.getJSONObject("0");**

现在我得到的每个值都是这样的:

String outletName = ssjson.getString("OutletName");

我的问题是有更好的方法(循环)通过所有对象(0,1,... 35)并分别获取每个单独对象的数据。

2 个答案:

答案 0 :(得分:0)

示例json

{
    "status": {
        "rcode": "200",
        "message": "Good, you got me!!!"
    },
    "data": [
        {
            "SubFranchiseID": "0",
            "OutletID": "607",
            "OutletName": "Spill",

        },
        {
            "SubFranchiseID": "0",
            "OutletID": "607",
            "OutletName": "Spill"
        }
    ]
}
  

使用getter和。用普通java类表示json   setter方法

首先创建一个根类(对于根对象)

public class Root {
  //For the status object
  private Status status;
  //For the array of data objects
  private List<Data> data;

  //create setters and getters for the two properties.
}

public class Status {

  private String rcode; 
  //this maps to the rcode in the status object in json
  private String message;
  //create setters and getters for the two
}

public class Data {
  private String SubFranchiseID;
  private String OutletID;
  private String OutletName;

//create setters and getters for these properties.
}

现在,让我们用gson(将json序列化到现成的类中)来实现魔力

reader.close();
json =sb.toString();

Root root = new Gson().fromJson(response, Root.class);
//get the rcode
if(root.getStatus().getrCode().equalsIgnoreCase("200"))
 {
   //print the data.
   for(Data d : root.getData()) {
      System.out.println(d.getSubFranchiseID()+"-"+d.getOutletID()+"-"+d.getOutletName());

   }
 }
  

使用gson的小注释。

 1.)  The property names inside the classes should match the property in the json or else use a `@SerializeName("propertyname")`.

 2.)  An array of objects would map to List<YourObject> , for eg : the data property in the json.

 3.)  You can have default value for your properties without creating getters and setters for them like 

private boolean isJson = true;

答案 1 :(得分:0)

感谢您的帮助。我在“数据”里面有很多json对象 - &gt;在Json Array(“Categories”)之前,“0”喜欢“Outletname”,“OutletID”等。我为我的数据创建了一个模型类。现在我想使用我的json数据的Gson.Part来重写json对象数据

  "0":{
 "SubFranchiseID":"0",
 "OutletID":"607",
 "OutletName":"Spill ",
 "BrandID":"403",
 "Address":"J-349, JP Road, Opp. Apna Bazar, JP Rd, D.N.Nagar, Andheri West, Mumbai, Maharashtra, India",
 "NeighbourhoodID":"1",
 "CityID":"1",
 "Email":null,
 "Timings":"Everyday: 6 pm to 1:30 am.",
 "CityRank":null,
 "Latitude":"19.127473",
 "Longitude":"72.832545",
 "Pincode":null,
 "Landmark":null,
 "Streetname":null,
 "BrandName":"Spill ",
 "OutletURL":"https:\/\/plus.google.com\/111539701326240643109\/about?hl=en-US",
 "NumCoupons":1,
 "NeighbourhoodName":"Andheri West",
 "PhoneNumber":"+91 22 2642 5895",
 "CityName":"Mumbai",
 "Distance":8205.2235,
 "Categories":[
    {
       "OfflineCategoryID":"32",
       "Name":"Continental",
       "ParentCategoryID":"1",
       "CategoryType":"Cuisine"
    },
    {
       "OfflineCategoryID":"13",
       "Name":"Bar and Restaurant",
       "ParentCategoryID":"1",
       "CategoryType":"TypeOfRestaurant"
    },
    {
       "OfflineCategoryID":"17",
       "Name":"Italian",
       "ParentCategoryID":"1",
       "CategoryType":"Cuisine"
    },
    {
       "OfflineCategoryID":"1",
       "Name":"Restaurant",
       "ParentCategoryID":null,
       "CategoryType":""
    },
    {
       "OfflineCategoryID":"21",
       "Name":"North Indian",
       "ParentCategoryID":"1",
       "CategoryType":"Cuisine"
    }
 ],
 "LogoURL":"http:\/\/www.google.in\/sitespecific\/media\/generated\/offlineimages\/logo_403.jpg",
 "CoverURL":"http:\/\/www.google.in\/sitespecific\/media\/generated\/offlineimages\/cover_607.jpg"
 },
}

我的java模型类:

        /**
         * SubFranchiseID : 0
         * OutletID : 607
         * OutletName : Spill
         * BrandID : 403
         * Address : J-349, JP Road, Opp. Apna Bazar, JP Rd,     D.N.Nagar, Andheri West, Mumbai, Maharashtra, India
         * NeighbourhoodID : 1
         * CityID : 1
         * Email : null
         * Timings : Everyday: 6 pm to 1:30 am.
         * CityRank : null
         * Latitude : 19.127473
         * Longitude : 72.832545
         * Pincode : null
         * Landmark : null
         * Streetname : null
         * BrandName : Spill
         * OutletURL : https://plus.google.com/111539701326240643109/about?hl=en-US
         * NumCoupons : 1
         * NeighbourhoodName : Andheri West
         * PhoneNumber : +91 22 2642 5895
         * CityName : Mumbai
         * Distance : 8205.2235
         * Categories : [{"OfflineCategoryID":"32","Name":"Continental","ParentCategoryID":"1","CategoryType":"Cuisine"},{"OfflineCategoryID":"13","Name":"Bar and Restaurant","ParentCategoryID":"1","CategoryType":"TypeOfRestaurant"},{"OfflineCategoryID":"17","Name":"Italian","ParentCategoryID":"1","CategoryType":"Cuisine"},{"OfflineCategoryID":"1","Name":"Restaurant","ParentCategoryID":null,"CategoryType":""},{"OfflineCategoryID":"21","Name":"North Indian","ParentCategoryID":"1","CategoryType":"Cuisine"}]
         * LogoURL : http://www.google.in/sitespecific/media/generated/offlineimages/logo_403.jpg
         * CoverURL : http://www.google.in/sitespecific/media/generated/offlineimages/cover_607.jpg
         */
public static class OutletDetailsEntity {
        private String SubFranchiseID;
        private String OutletID;
        private String OutletName;
        private String BrandID;
        private String Address;
        private String NeighbourhoodID;
        private String CityID;
        private Object Email;
        private String Timings;
        private Object CityRank;
        private String Latitude;
        private String Longitude;
        private Object Pincode;
        private Object Landmark;
        private Object Streetname;
        private String BrandName;
        private String OutletURL;
        private int NumCoupons;
        private String NeighbourhoodName;
        private String PhoneNumber;
        private String CityName;
        private double Distance;
        private String LogoURL;
        private String CoverURL;
        private List<CategoriesEntity> Categories;

        public void setSubFranchiseID(String SubFranchiseID) {
            this.SubFranchiseID = SubFranchiseID;
        }

        public void setOutletID(String OutletID) {
            this.OutletID = OutletID;
        }

        public void setOutletName(String OutletName) {
            this.OutletName = OutletName;
        }

        public void setBrandID(String BrandID) {
            this.BrandID = BrandID;
        }

        public void setAddress(String Address) {
            this.Address = Address;
        }

        public void setNeighbourhoodID(String NeighbourhoodID) {
            this.NeighbourhoodID = NeighbourhoodID;
        }

        public void setCityID(String CityID) {
            this.CityID = CityID;
        }

        public void setEmail(Object Email) {
            this.Email = Email;
        }

        public void setTimings(String Timings) {
            this.Timings = Timings;
        }

        public void setCityRank(Object CityRank) {
            this.CityRank = CityRank;
        }

        public void setLatitude(String Latitude) {
            this.Latitude = Latitude;
        }

        public void setLongitude(String Longitude) {
            this.Longitude = Longitude;
        }

        public void setPincode(Object Pincode) {
            this.Pincode = Pincode;
        }

        public void setLandmark(Object Landmark) {
            this.Landmark = Landmark;
        }

        public void setStreetname(Object Streetname) {
            this.Streetname = Streetname;
        }

        public void setBrandName(String BrandName) {
            this.BrandName = BrandName;
        }

        public void setOutletURL(String OutletURL) {
            this.OutletURL = OutletURL;
        }

        public void setNumCoupons(int NumCoupons) {
            this.NumCoupons = NumCoupons;
        }

        public void setNeighbourhoodName(String NeighbourhoodName) {
            this.NeighbourhoodName = NeighbourhoodName;
        }

        public void setPhoneNumber(String PhoneNumber) {
            this.PhoneNumber = PhoneNumber;
        }

        public void setCityName(String CityName) {
            this.CityName = CityName;
        }

        public void setDistance(double Distance) {
            this.Distance = Distance;
        }

        public void setLogoURL(String LogoURL) {
            this.LogoURL = LogoURL;
        }

        public void setCoverURL(String CoverURL) {
            this.CoverURL = CoverURL;
        }

        public void setCategories(List<CategoriesEntity> Categories) {
            this.Categories = Categories;
        }

        public String getSubFranchiseID() {
            return SubFranchiseID;
        }

        public String getOutletID() {
            return OutletID;
        }

        public String getOutletName() {
            return OutletName;
        }

        public String getBrandID() {
            return BrandID;
        }

        public String getAddress() {
            return Address;
        }

        public String getNeighbourhoodID() {
            return NeighbourhoodID;
        }

        public String getCityID() {
            return CityID;
        }

        public Object getEmail() {
            return Email;
        }

        public String getTimings() {
            return Timings;
        }

        public Object getCityRank() {
            return CityRank;
        }

        public String getLatitude() {
            return Latitude;
        }

        public String getLongitude() {
            return Longitude;
        }

        public Object getPincode() {
            return Pincode;
        }

        public Object getLandmark() {
            return Landmark;
        }

        public Object getStreetname() {
            return Streetname;
        }

        public String getBrandName() {
            return BrandName;
        }

        public String getOutletURL() {
            return OutletURL;
        }

        public int getNumCoupons() {
            return NumCoupons;
        }

        public String getNeighbourhoodName() {
            return NeighbourhoodName;
        }

        public String getPhoneNumber() {
            return PhoneNumber;
        }

        public String getCityName() {
            return CityName;
        }

        public double getDistance() {
            return Distance;
        }

        public String getLogoURL() {
            return LogoURL;
        }

        public String getCoverURL() {
            return CoverURL;
        }