我已经使用片段进行了编码。
这是我的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: aya.b@fcc.bh 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 & GUY",
slug: "tony-and-guy",
description: "TONI&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: "ahmed@goodfood.bh",
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 :(得分: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);