我试图解析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)并分别获取每个单独的对象的数据。
您应该使用GSON,编写所有的锅炉板JSON解析自己。话虽如此,请至少提供您期望在响应中的准确json,这是有效的,因为这似乎是无效的json结构。 – cafebabe1991
我编辑了json数据,并且尝试使用** Iterator > keys = resobj.keys(); ** ande通过while循环迭代键。由于json数据的名称为“0”,Android Studio无法将json转换为Java Object类。您可以给出有关如何使用Gson解析的深入见解 – Evani