簡體   English   中英

在android中解析嵌套的json對象

[英]parsing nested json object in android

當以嵌套的Json對象形式返回Json響應數據時,如何解析Json數據。

我看了這篇文章 ,展示了如何只獲取一個數據。 因為我想要整個數據集對我來說沒用。

它說:

JSONObject jMainObject = null;
    try {
        jMainObject = new JSONObject(jsonString);
        JSONObject apiGroupsObject = jMainObject.getJSONObject("apiGroups");
        JSONObject affiliateObject = apiGroupsObject.getJSONObject("affiliate"); 
        JSONObject apiListingsObject = affiliateObject.getJSONObject("apiListings");
        JSONObject bags_wallets_beltsObject = apiListingsObject.getJSONObject("bags_wallets_belts");
        JSONObject availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
        JSONObject versionObject = availableVariantsObject.getJSONObject("v0.1.0");
        System.out.println(versionObject);
    } catch (JSONException e) {
        e.printStackTrace();
    }

Json返回我要解析的數據來自這里

任何人都可以幫助我解決我的問題,如何解析這種類型的數據,因為我是新的,所以我不知道如何解析嵌套的Json對象數據。 也許我可以用Gson嗎? 但我也不知道如何使用它。

序列化/反序列化的第一條規則 - 不要自己動手。 請改用編譯'com.google.code.gson:gson:2.6.2' 它是基於谷歌注釋的反序列化器。 這是一些如何使用它的教程。

Try your parsing using volley library .It's a big data so always use optJson object for avoiding null.This is small piece of code .Hope it will help you.
  String title = response.getString("title");
String   description = response.getString("description");


JSONObject objApiGrp = response.optJSONObject("apiGroups");
if(objApiGrp != null && objApiGrp.size()>0)
{
    JSONObject objaffiliate = objApiGrp.optJSONObject("affiliate");

    for(int i=0 ; i<objaffiliate.size() ; i++)
{


JSONObject objapiListings = objaffiliate.optJSONObject("apiListings");
for(int i=0 ; i<objapiListings.size() ; i++)
{

//      parse all objects


}


}

您需要創建所有單獨的對象以獲取整個關鍵數據值。 像這樣:

JSONObject jMainObject = null;
try {
    jMainObject = new JSONObject(jsonString);
    JSONObject apiGroupsObject = jMainObject.getJSONObject("apiGroups");
    JSONObject affiliateObject = apiGroupsObject.getJSONObject("affiliate"); 
    JSONObject apiListingsObject = affiliateObject.getJSONObject("apiListings");
    JSONObject bags_wallets_beltsObject = apiListingsObject.getJSONObject("bags_wallets_belts");
    JSONObject availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
    JSONObject versionObject = availableVariantsObject.getJSONObject("v0.1.0");
    System.out.println(versionObject);

    JSONObject fragrances = apiListingsObject.getJSONObject("fragrances");
    availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
    versionObject = availableVariantsObject.getJSONObject("v0.1.0");
    System.out.println(versionObject);

    JSONObject tv_video_accessories = apiListingsObject.getJSONObject("tv_video_accessories");
    availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
    versionObject = availableVariantsObject.getJSONObject("v0.1.0");
    System.out.println(versionObject);

    JSONObject camera_accessories = apiListingsObject.getJSONObject("camera_accessories");
    availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
    versionObject = availableVariantsObject.getJSONObject("v0.1.0");
    System.out.println(versionObject);

    JSONObject sports_fitness = apiListingsObject.getJSONObject("sports_fitness");
    availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
    versionObject = availableVariantsObject.getJSONObject("v0.1.0");
    System.out.println(versionObject);

    // Make and use object like this for all key Values.

} catch (JSONException e) {
    e.printStackTrace();
}

所有鍵都不同,所以不能使用循環。 否則代碼是用循環優化的。

do it like this, hope it will help you, tested    

JSONObject objresponse = new JSONObject(response);
                    JSONObject objapiGroups = objresponse.getJSONObject("apiGroups");
                    JSONObject objaffiliate = objapiGroups.getJSONObject("affiliate");
                    JSONObject objapiListings = objaffiliate.getJSONObject("apiListings");

                    Iterator iterator = objapiListings.keys();
                    int i=0;
                    Catagory=new String[objapiListings.length()];
                    while(iterator.hasNext()){
                        String key = (String)iterator.next();
                        JSONObject issue = objapiListings.getJSONObject(key);

                        //  get id from  issue
                        Catagory[i] = issue.optString("apiName");
                        i++;
                    }
                    JSONObject[] objcat = new JSONObject[Catagory.length];
                    CatagoryName=new String[Catagory.length];
                    Url=new String[Catagory.length];
                    for(int j=0;j<Catagory.length;j++)
                    {
                        objcat[j]=objapiListings.getJSONObject(Catagory[j]);

                        CatagoryName[j]=objcat[j].getJSONObject("availableVariants").getJSONObject("v1.1.0").getString("resourceName");
                        Url[j]=objcat[j].getJSONObject("availableVariants").getJSONObject("v1.1.0").getString("get");

                    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM