简体   繁体   中英

How to convert JsonArray to ArrayList but JsonObject In JsonArray is not the same structure

How can I convert below JsonArray with different JsonObject's into Single Arraylist

[{
    "type": "textCard",
    "data": {
        "dataType": "TextCard",
        "id": 0,
        "type": "header5",
        "text": "",
        "subTitle": null,
        "actionUrl": null,
        "adTrack": null,
        "follow": null
    },
    "tag": null,
    "id": 0,
    "adIndex": -1
},
{
    "type": "briefCard",
    "data": {
        "dataType": "BriefCard",
        "id": 30,
        "icon": "*jpeg",
        "iconType": "square",
        "title": "#",
        "subTitle": null,
        "description": "",
        "actionUrl": "B8%B8%E6%88%8F",
        "adTrack": null,
        "follow": {
            "itemType": "category",
            "itemId": 30,
            "followed": false
        },
        "ifPgc": false
    },
    "tag": null,
    "id": 0,
    "adIndex": -1
}]

try this ,

 try {
                JSONArray array=new JSONArray(res);
                if (array.length()>0){
                    for (int i=0;i<array.length();i++){
                        JSONObject object1=array.getJSONObject(i);

                        JSONObject object2=object1.getJSONObject("data");

                        // do this for your needed objects

                        if (object2.has("icon")){
                            Log.d("icon",object2.getString("icon"));
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM