简体   繁体   中英

How to access JSON element which is an array

I have the GSON library in my project, I need the ext_id. I am able to access "images" using the JSON.get("images"), however I am unable to get into the images array. Can someone please help me with this.

{
    "images": [{
    "attributes": {},
    "ext_id": "467152316",
    "flag_count": 0,
    "height": 2064,
    "id": "da2d2307b6b72afcfbd6eab8287fad49fce442d6",
    "platform": "Web",
    "state_name": "public",
    "type_name": "ypid",
    "user_id": 7297580,
    "user_type": "obile",
    "width": 1161,
    "verified": 1,
    "blob_media_type": "image",
    "created_at": "2016-08-19 19:29:13 +0000",
    "deleted": 0,
    "end_date": "2216-07-02 19:29:13 +0000",
    "primary": 0,
    "recurring": 0,
    "start_date": "2016-08-19 19:29:13 +0000",
    "state": 0,
    "tags": [],
    "type_id": 1,
    "updated_at": "2016-08-19 19:29:13 +0000",
    "user": "Fanta C.",
    "business": {
        "name": "Porto's Bakery",
        "ypid": "467152316",
        "listing_id": "467152316",
        "primary_collection": "food",
        "heading_code": "8004219",
        "mip_url": "/glendale-ca/mip/portos-bakery-467152316",
        "phone_number": "(818) 956-5996",
        "address": "315 N Brand Blvd",
        "heading_text": "Dessert Restaurants",
        "city": "Glendale",
        "state": "CA",
        "zip": "91203",
        "is_paid": false,
        "tier": 999,
        "source": "MDM",
        "rating_attributes": [{
            "name": "Atmosphere",
            "id": 1031
        }, {
            "name": "Food",
            "id": 1023
        }, {
            "name": "Service",
            "id": 1038
        }]
    }
}],
"count": 1

}

You can try this one.

public void parse(String jsonString) {
    JsonElement jelement = new JsonParser().parse(jsonString);
    JsonObject jobject = jelement.getAsJsonObject();
    JsonArray jarray = jobject.getAsJsonArray("images");
    for(int i=0;i<jarray.size();i++){
        JsonObject jsonObject = jarray.get(i).getAsJsonObject();
        String result = jsonObject.get("ext_id").toString();
        System.out.println(result);
        result = jsonObject.get("height").toString();
        System.out.println(result);
    }
}

You can also look here

   JsonArray jsonArray = (JsonArray)jObject3.get("images");
   JsonObject jObject4 =(JsonObject) jsonArray.get(0);
   System.out.println(jObject4.get("ext_id"));
   System.out.println(jObject4.get("full_image_path"));

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