如何检索JSONArray中的JSONOBJECT

时间:2015-03-28 13:35:01

标签: android json

这是我的Java代码,它是从JSON检索数据

     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("slug"));
                            data.setMsg(object.getString("modified"));
                            data.setLocation(object.getString("type"));

// I want to show the data inside the offer_shop_details.

//喜欢afflshop_address,afflshop_email,请帮帮我。

JSONObject authore = object.getJSONObject("offer_shop_details");

String authore1 = authore.getString("afflshop_address");
data.setLocation(object.getString(authore1));

                            itemList.add(data);
                        }

这是我没有OBJECT NAME的JSONARRAY我检索了JSONARRAY中的数据。我无法检索JSONOBJECT中的数据,这是在我的主JSONARRAY中。以下是我的代码片段。

   [{
    ID: 482,
    title: "Sample Offer",
    status: "publish",
    type: "offers",
    author: {
    ID: 1,
    username: "bm",
    name: "bm",
    first_name: "",
    last_name: "",
    nickname: "bm",
    slug: "bm",
    URL: "",
    avatar: "http://0.gravatar.com/avatar/81ff187fd3ab62858b098258fb3f0479?s=96",
    description: "",
    registered: "2015-01-11T07:25:06+00:00",
    meta: {
    links: {
    self: "http://titusandbindu.com/bm/wp-json/users/1",
    archives: "http://titusandbindu.com/bm/wp-json/users/1/posts"
    }
    }
    },
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: "http://titusandbindu.com/bm/organic-foods-and-cafe/",
afflshop_email: "ahmed@goodfood.bh",
afflshop_contact: "3333286",
afflshop_image_id: {
id: "512",
url: "http://titusandbindu.com/bm/wp-content/uploads/2015/03/test1.jpg"
}
}
]

2 个答案:

答案 0 :(得分:1)

JSON是用于示例目的还是您的实际JSON输出?如果它是你的输出,那么你的问题是它是无效的JSON。您的所有字段都需要为字符串...“ID”:482等。此外,您错过了结束“}”。

Cut&amp;将您的JSON粘贴到jsonlint.com并点击“验证”;)

答案 1 :(得分:0)

您的json对象错误: 应该是这样的:

[
  {
    ID: 482,
    title: "Sample Offer",
    status: "publish",
    type: "offers",
    author: {
      ID: 1,
      username: "bm",
      name: "bm",
      first_name: "",
      last_name: "",
      nickname: "bm",
      slug: "bm",
      URL: "",
      avatar: "http://0.gravatar.com/avatar/81ff187fd3ab62858b098258fb3f0479?s=96",
      description: "",
      registered: "2015-01-11T07:25:06+00:00",
      meta: {
        links: {
          self: "http://titusandbindu.com/bm/wp-json/users/1",
          archives: "http://titusandbindu.com/bm/wp-json/users/1/posts"
        }
      }
    }
  }
] 

解决方案:

     res = out.toString();
                                JSONArray arrJsonArray=new JSONArray(res);
                                for (int i = 0; i < arrJsonArray.length(); i++) {
**JSONObject object =arrJsonArray.getJSONObject(i).getJSONObject("author");**
                                   //here is your next logic




                                    itemList.add(data);
                                }