2015-09-27 93 views
1
循环

我试图解析JSON对象,部分看起来是这样的:通过多个JSON对象中的Android

{ 
    "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 ", 
     . 
     . 
     . 
     } 
} 

个分别有这些objects.I的近35 JSON对象和嵌套JsonArrays “M试图得到这样的数据:

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

您应该使用GSON,编写所有的锅炉板JSON解析自己。话虽如此,请至少提供您期望在响应中的准确json,这是有效的,因为这似乎是无效的json结构。 – cafebabe1991

+0

我编辑了json数据,并且尝试使用** Iterator keys = resobj.keys(); ** ande通过while循环迭代键。由于json数据的名称为“0”,Android Studio无法将json转换为Java Object类。您可以给出有关如何使用Gson解析的深入见解 – Evani

回答

0

例JSON

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

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

代表在普通的Java类与吸气剂的术语和 制定者

的JSON

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

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; 
+0

感谢您的帮助。在Json Array(“Categories”)之前,我有很多像“Outletname”,“OutletID”等“data” - >“0”中的json对象。我为我的数据创建了一个模型类。现在我想使用Gson来读取json对象的数据。 – Evani

+0

这很简单..你看到我写的for循环,这是迭代数据的方式。 – cafebabe1991

0

感谢您的帮助。在Json Array(“Categories”)之前,我有很多像“Outletname”,“OutletID”等“data” - >“0”中的json对象。我为我的数据创建了一个模型类。现在,我想用我的JSON数据的Gson.Part

"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模型类,以itterate JSON对象数据:

 /** 
     * 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; 
     }