在数组中解析嵌套数组

时间:2015-08-27 08:47:21

标签: android arrays json parsing nested

我通常使用Retrofit进行所有解析"脏"工作,但最近我决定用手解析json"。而且我无法弄清楚如何解析数组中的嵌套数组,还有我的json:

[
   {
      "title":"Phone",
      "subs":[
         {
            "id":157291,
            "title":"Cell Phone Service"
         },
         {
            "id":524624,
            "title":"Landline Phone Service"
         },
         {
            "id":157298,
            "title":"Voice Over IP"
         }
      ]
   },
   {
      "title":"TV and Internet",
      "subs":[
         {
            "id":157292,
            "title":"Hardwire Internet"
         },
         {
            "id":178472,
            "title":"Television"
         },
         {
            "id":524625,
            "title":"Wireless Internet"
         }
      ]
   },
   {
      "title":"Entertainment",
      "subs":[
         {
            "id":522695,
            "title":"Music and Movies"
         }
      ]
   },
   {
      "title":"Bill Payment",
      "subs":[
         {
            "id":179845,
            "title":"Home Utilities"
         }
      ]
   },
   {
      "title":"Games and Social",
      "subs":[
         {
            "id":157297,
            "title":"Games",
            "subs":[
               {
                  "id":525000,
                  "title":"Category:Casual"
               },
               {
                  "id":525001,
                  "title":"Category:Online Games"
               },
               {
                  "id":525002,
                  "title":"Category:Action and Shooter Games"
               },
               {
                  "id":525003,
                  "title":"Category:RPG"
               },
               {
                  "id":525005,
                  "title":"Category:Strategy"
               },
               {
                  "id":525006,
                  "title":"Category:Adventure"
               },
               {
                  "id":525008,
                  "title":"Category:Simulators"
               },
               {
                  "id":525171,
                  "title":"Category:Portals and Services"
               },
               {
                  "id":525265,
                  "title":"Category:Game artefacts"
               }
            ]
         },
         {
            "id":524626,
            "title":"Social Networks"
         },
         {
            "id":522901,
            "title":"Dating"
         }
      ]
   },
   {
      "title":"Finances",
      "subs":[
         {
            "id":522747,
            "title":"Loan Repayment"
         }
      ]
   },
   {
      "title":"Everyday Purchases",
      "subs":[
         {
            "id":288993,
            "title":"Beauty, Health, and Sports"
         }
      ]
   },
   {
      "title":"Travel",
      "subs":[
         {
            "id":523297,
            "title":"Travel Reservations"
         },
         {
            "id":524634,
            "title":"Travel Agencies"
         }
      ]
   },
   {
      "title":"Websites",
      "subs":[
         {
            "id":160550,
            "title":"Advertising"
         },
         {
            "id":233554,
            "title":"Domain Hosting"
         }
      ]
   },
   {
      "title":"And also:",
      "subs":[
         {
            "id":179843,
            "title":"Charity"
         },
         {
            "id":524635,
            "title":"Online auctions"
         },
         {
            "id":522887,
            "title":"Miscellaneous"
         }
      ]
   }
]

我使用这段代码获得了第二级嵌套数组,但是如何获得第三级数组?

    String response = RequestManager.makeRequest();
    StringBuilder sbResponse = new StringBuilder();

    try {
        JSONArray jsonArray = new JSONArray(response);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONArray nestedArray = jsonObject.getJSONArray("subs");
            for (int j = 0; j < nestedArray.length(); j++) {

            }
        }
        Log.d(LOG_TAG, sbResponse.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

顺便说一句,我应该如何将这些数据存储在数据库中 - 我使用Realm并且我已经创建了Category和SubCategory模型,我是否需要创建另一个子类别模型来保存数据第三级数组?

分类模型:

public class Category extends RealmObject {

    @PrimaryKey
    private String title;
    private SubCategory subCategory;

    //Getters and setters
}

和SubCategory模型:

public class SubCategory extends RealmObject {

    @PrimaryKey
    private int id;
    private String title;

    //Getters and setters
}

2 个答案:

答案 0 :(得分:1)

String response = RequestManager.makeRequest();
    StringBuilder sbResponse = new StringBuilder();

    try {
        JSONArray jsonArray = new JSONArray(response);
        for (int i = 0; i < jsonArray.length(); i++) {
            Category c=new Category();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            c.setTitle(jsonObject.getString("title"));
            JSONArray nestedArray = jsonObject.getJSONArray("subs");
            for (int j = 0; j < nestedArray.length(); j++) {
                 SubCategory s=new SubCategory();
                 JSONObject nestedObject= nestedArray.getJSONObject(i);
                 s.setId(nestedObject.getString("id"));
                 s.setTitle(nestedObject.getString("title"));
            }
            c.setSubCategory(s);
        }
        Log.d(LOG_TAG, sbResponse.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

答案 1 :(得分:1)

try {
    JSONArray jsonArray = new JSONArray(response);
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        JSONArray nestedArray = jsonObject.getJSONArray("subs");
        for (int j = 0; j < nestedArray.length(); j++) {
            JSONObject nestObj = nestedArray.getJSONObject(j);
            if(nestObj.has("subs"))
            {
                JSONArray insideArray = nestObj.getJSONArray("subs");
                 for (int k = 0; k < insideArray .length(); k++) {

                 }
            }
        }
    }
    Log.d(LOG_TAG, sbResponse.toString());
} catch (JSONException e) {
    e.printStackTrace();
}