【问题标题】:how to parse this nested json response?如何解析这个嵌套的 json 响应?
【发布时间】:2016-01-28 06:21:13
【问题描述】:

我收到这种格式的 json 响应。

{
   "apiGroups":
   {
       "Affiliate":
       {
           "listingsAvailable":
           {
               "Beauty_Personal_Care":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:586:821655440?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },
               "Eyewear":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:473:662748456716?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },
               "Real_Estate":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:897:673143570606?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },
               "Jewellery":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:6:315773046?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },
               "Furniture":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:580:1894930153?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },
               "Tweens_Boys":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:814:934253466?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },
               "Automobiles":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:1145:639299259208?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },
               "Home_Improvement":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:864:624389489778?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },
               "The_Designer_Studio":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:924:655684426383?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },
               "Fashion_Jewellery":
               {
                   "listingVersions":
                   {
                       "v1":
                       {
                           "get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:1113:672114192240?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
                       }
                   }
               },

我需要在获取字段中获取美容个人护理、眼用品等类别以及它们各自的 url。我怎样才能遍历这个并获取。到目前为止,我尝试过这样,但不知道下一步如何进行。谁能给我建议如何解析这个 json?

json = jParser.getJSONFromUrl(response);
JSONObject api = json.getJSONObject("apiGroups");
JSONObject affiliate = api.getJSONObject("Affiliate");
JSONObject list = affiliate.getJSONObject("listingsAvailable");

【问题讨论】:

标签: java android json


【解决方案1】:

您可以阅读有关 Android 中 JSONObject 类的 documentation。 在本文档中,您将找到 keys 方法,该方法将“返回此对象中字符串名称的迭代器。”

所以你只需要调用这个方法并使用迭代器。

Iterator<String> keysIterator = jsonObject.keys();
String key;
while (keysIterator.hasNext()) {
     key = keysIterator.next();
     //use the key to retrieve the data form jsonObject
}

但是,如果您是生成此 json 的人,则可以考虑对其进行一些更改。 listingsAvailable 中的数据可能应该在一个数组中。

【讨论】:

    猜你喜欢
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2021-03-17
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多