0

I have the following JSON object below and I was wondering how to fetch the results of MediaUrl. I found a lot of tutorials on how to parse an array or an object but I can't find anything on how to loop an array inside of an object.

{
"d": {
    "results": [
        {
            "__metadata": {
                "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=0&$top=1",
                "type": "ImageResult"
            },
            "ID": "17866c98-31f2-4488-9512-e47785301117",
            "Title": "acanthurus achilles acanthuridae poissons chirurgiens",
            "MediaUrl": "http://www.aquariophilie62.fr/images/poissons-recifal/thumbs/Acanthurus-achilles-.jpg",
            "SourceUrl": "http://www.aquariophilie62.fr/fiches-poissons-recifaux/",
            "DisplayUrl": "www.aquariophilie62.fr/fiches-poissons-recifaux",
            "Width": "100",
            "Height": "100",
            "FileSize": "19633",
            "ContentType": "image/jpeg",
            "Thumbnail": {
                "__metadata": {
                    "type": "Bing.Thumbnail"
                },
                "MediaUrl": "http://ts2.mm.bing.net/th?id=H.4820238373750001&pid=15.1",
                "ContentType": "image/jpg",
                "Width": "100",
                "Height": "100",
                "FileSize": "2509"
            }
        },
        {
            "__metadata": {
                "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=1&$top=1",
                "type": "ImageResult"
            },
            "ID": "e2a96fba-f8b2-4212-8477-1bf36131d61c",
            "Title": "100x100px-LS-8c425bd7_47876-000676_Acanthurus_achilles.jpg",
            "MediaUrl": "http://cdn.saltwaterfish.com/8/8c/100x100px-LS-8c425bd7_47876-000676_Acanthurus_achilles.jpg",
            "SourceUrl": "http://forums.saltwaterfish.com/t/346818/humu-humu-trigger-shrimps",
            "DisplayUrl": "forums.saltwaterfish.com/t/346818/humu-humu-trigger-shrimps",
            "Width": "100",
            "Height": "100",
            "FileSize": "8185",
            "ContentType": "image/jpeg",
            "Thumbnail": {
                "__metadata": {
                    "type": "Bing.Thumbnail"
                },
                "MediaUrl": "http://ts2.mm.bing.net/th?id=H.4781768365638869&pid=15.1",
                "ContentType": "image/jpg",
                "Width": "100",
                "Height": "100",
                "FileSize": "2234"
            }
        },
        {
            "__metadata": {
                "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=2&$top=1",
                "type": "ImageResult"
            },
            "ID": "11d79168-8fd1-49d9-9d06-c54208cecf28",
            "Title": "acanthurus bahianus acanthuridae poissons chirurgiens",
            "MediaUrl": "http://www.aquariophilie62.fr/images/poissons-recifal/thumbs/Acanthurus-bahianus-.jpg",
            "SourceUrl": "http://www.aquariophilie62.fr/fiches-poissons-recifaux/",
            "DisplayUrl": "www.aquariophilie62.fr/fiches-poissons-recifaux",
            "Width": "100",
            "Height": "100",
            "FileSize": "19105",
            "ContentType": "image/jpeg",
            "Thumbnail": {
                "__metadata": {
                    "type": "Bing.Thumbnail"
                },
                "MediaUrl": "http://ts2.mm.bing.net/th?id=H.4820238373749985&pid=15.1",
                "ContentType": "image/jpg",
                "Width": "100",
                "Height": "100",
                "FileSize": "2373"
            }
        }
    ],
    "__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus%20achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=3&$top=3"
}

}

1 Answer 1

1

Assuming obj is a JSONObject obtained by parsing the posted String

"results" is the key to a JSONArray that you get from your object using

JSONArray results = obj.getJSONObject("d").getJSONArray("results")

JSONArray has get*(int index) for each type of data you can encounter.

In your case, "results" is an array of JSONObject. You can loop through it using :

for (int i = 0; i < results.length(); i++) {
    JSONObject oneResult = results.getJSONObject(i);
}

Once you have the JSONObject, the MediaUrl is just one getString("MediaUrl") away ...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is exactly what I was looking for. Looking at the code it makes perfect sense and works like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.