2015-03-31 49 views
-1

我已经完成了使用fragment的编码。下面如何检索json数组内部的对象值

这个我homeFragment

res = out.toString(); 
         JSONArray arrJsonArray=new JSONArray(res); 
         for (int i = 0; i < arrJsonArray.length(); i++) { 
          JSONObject object = arrJsonArray.getJSONObject(i); 
          Data data=new Data(); 
          data.setName(object.getString("title")); 
          data.setMsg(object.getString("slug")); 
          /* for abouve code i am getting data for below code i am not able to retrive the data*/ 
          object.getJSONObject("offer_shop_details"); 
          data.setOffer(object.getString("time_field_from_sunday")); 
          itemList.add(data); 
         } 

是我的JSON。我是jsonArray

我给了我的json的一个数组元素。

我需要获取“offer_shop_details”中的数据,以上是我的逻辑。帮助我找到正确的答案。

下面的json如果你验证它会告诉无效。因为我只给了它的一部分。

  [ 
{ 
ID: 528, 
title: "Buy breakfast meal get free salad", 
status: "publish", 
type: "offers", 
content: "", 
parent: 0, 
link: "", 
date: "2015-03-30T10:25:29+00:00", 
slug: "buy-breakfast-meal-get-free-salad-2", 
excerpt: null, 
meta: { 
links: { 
self: "", 
author: "", 
collection: "", 
replies: "", 
version-history: "" 
} 
}, 
offer_shop_details: { 
time_field_from: "03:00", 
time_field_to: "18:45", 
time_field_from_sunday: "10:00", 
time_field_to_sunday: "00:00", 
time_field_from_monday: "10:00", 
time_field_to_monday: "00:00", 
time_field_from_tuesday: "10:00", 
time_field_to_tuesday: "00:00", 
time_field_from_wednesday: "10:00", 
time_field_to_wednesday: "00:00", 
time_field_from_thursday: "10:00", 
time_field_to_thursday: "00:00", 
time_field_from_friday: "10:00", 
time_field_to_friday: "00:00", 
time_field_from_saturday: "10:00", 
time_field_to_saturday: "00:00", 
afflshop_address: "Manama", 
afflshop_website: "", 
afflshop_image_id: { 
id: "278", 
url: "" 
} 
}, 
mall_shop_details: false, 
author: { 
ID: 1, 
username: "bm", 
name: "bm", 
first_name: "", 
last_name: "", 
nickname: "bm", 
slug: "bm", 
URL: "", 
avatar: "", 
description: "", 
registered: "2015-01-11T07:25:06+00:00", 
meta: { 
links: { 
self: "", 
archives: "" 
} 
} 
}, 
featured_image: null, 
terms: { 
offershop: [ 
{ 
ID: 29, 
name: "Salad boutique", 
slug: "salad-boutique", 
description: "", 
parent: { 
ID: 31, 
name: "Restaurant", 
slug: "restaurant", 
description: "", 
parent: null, 
count: 0, 
link: "", 
meta: { 
links: { 
collection: "", 
self: "" 
} 
} 
}, 
count: 1, 
link: "", 
meta: { 
links: { 
collection: "", 
self: "" 
} 
} 
} 
] 
} 
}, 
{ 
ID: 527, 
title: "One Full Service Second for Half Price", 
status: "publish", 
type: "offers", 
content: "", 
parent: 0, 
link: "", 
date: "2015-03-30T10:21:17+00:00", 
slug: "one-full-service-second-for-half-price-2", 
excerpt: null, 
meta: { 
links: { 
self: "", 
author: "", 
collection: "", 
replies: "", 
version-history: "" 
} 
}, 
offer_shop_details: { 
time_field_from_sunday: "10:00", 
time_field_to_sunday: "22:00", 
time_field_from_monday: "10:00", 
time_field_to_monday: "22:00", 
time_field_from_tuesday: "10:00", 
time_field_to_tuesday: "22:00", 
time_field_from_wednesday: "10:00", 
time_field_to_wednesday: " 22:00", 
time_field_from_thursday: "10:00", 
time_field_to_thursday: "22:00", 
time_field_from_friday: "10:00", 
time_field_to_friday: "22:00", 
time_field_from_saturday: "10:00", 
time_field_to_saturday: "22:00", 
afflshop_address: "Name: Aya EL Baba Email: [email protected] Address: SEEF Location: Adliya", 
afflshop_website: "", 
afflshop_contact: "+. 973 36 9999 25", 
afflshop_image_id: { 
id: "279", 
url: "" 
} 
}, 
mall_shop_details: false, 
author: { 
ID: 1, 
username: "bm", 
name: "bm", 
first_name: "", 
last_name: "", 
nickname: "bm", 
slug: "bm", 
URL: "", 
avatar: "", 
description: "", 
registered: "2015-01-11T07:25:06+00:00", 
meta: { 
links: { 
self: "", 
archives: "" 
} 
} 
}, 
featured_image: null, 
terms: { 
offershop: [ 
{ 
ID: 36, 
name: "TONY &amp; GUY", 
slug: "tony-and-guy", 
description: "TONI&amp;GUY is the most powerful hairdressing brand in the world. The company is constantly evolving artistically and commercially. It offers the best education on the planet and boasts fabulous product ranges and a dynamic and modern image.", 
parent: { 
ID: 35, 
name: "Saloon", 
slug: "saloon", 
description: "", 
parent: null, 
count: 0, 
link: "", 
meta: { 
links: { 
collection: "", 
self: "" 
} 
} 
}, 
count: 1, 
link: "", 
meta: { 
links: { 
collection: "", 
self: "" 
} 
} 
} 
] 
} 
}, 
{ 
ID: 482, 
title: "Sample Offer", 
status: "publish", 
type: "offers", 
content: "", 
parent: 0, 
link: "", 
date: "2015-03-26T10:48:35+00:00", 
slug: "sample-offer-10", 
excerpt: null, 
meta: { 
links: { 
self: "", 
author: "", 
collection: "", 
replies: "", 
version-history: "" 
} 
}, 
offer_shop_details: { 
afflshop_days: [ 
"0", 
"1", 
"2", 
"3", 
"4", 
"5", 
"6" 
], 
time_field_from: "09:00", 
time_field_to: "22:00", 
afflshop_address: "New extention Seef Mall, 1st Floor, Manama, Bahrain", 
afflshop_website: "", 
afflshop_email: "[email protected]", 
afflshop_contact: "3333286", 
afflshop_image_id: { 
id: "512", 
url: "" 
} 
}, 
mall_shop_details: false, 
author: { 
ID: 1, 
username: "bm", 
name: "bm", 
first_name: "", 
last_name: "", 
nickname: "bm", 
slug: "bm", 
URL: "", 
avatar: "", 
description: "", 
registered: "2015-01-11T07:25:06+00:00", 
meta: { 
links: { 
self: "", 
archives: "" 
} 
} 
}, 
featured_image: null, 
terms: { 
offershop: [ 
{ 
ID: 62, 
name: "Organic Foods and Café", 
slug: "organic-foods-and-cafe", 
description: "", 
parent: { 
ID: 31, 
name: "Restaurant", 
slug: "restaurant", 
description: "", 
parent: null, 
count: 0, 
link: "", 
meta: { 
links: { 
collection: "", 
self: "" 
} 
} 
}, 
count: 1, 
link: "", 
meta: { 
links: { 
collection: "", 
self: "" 
} 
} 
} 
] 
} 
} 
] 

回答

0

您的JSON字符串的根是一个JSONObject因此用它来分析你的JSON,然后用getJSONArray让你的JSON数组:

res = out.toString(); 

JSONObject rootJsonObject = new JSONObject(res); 

// And then you can access to your array 
JSONArray arrJsonArray= jsonObject.getJSONArray(rootJsonObject); 
+0

我的根是JSONArray不JSONObject的。我刚才把0的数组放在上面。我的json以[大括号开头,并立即用{this。我不能把完整的链接。 – 2015-03-31 10:18:59