如何解析这个嵌套的json响应?

时间:2015-10-28 12:57:21

标签: java android json

我正在以这种格式获得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"
                       }
                   }
               },

我需要在get field中获得美容个人护理,眼器和各自网址等类别。我怎样才能完成这个并得到它。所以我试着这样做,不知道如何继续下去。任何人都可以给我建议如何解析这个json?

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

1 个答案:

答案 0 :(得分:1)

您可以阅读有关Android中JSONObject课程的documentation。 在本文档中,您将找到方法keys,它将"返回此对象中String名称的迭代器。"

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

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

但是,如果你是生成这个json的那个,你可以考虑稍微改一下。 listingsAvailable 中的数据可能应该在数组中。