MLProgrammer-CiM
MLProgrammer-CiM

Reputation: 17257

Get value from deep inside a JSONObject

I have this Bing Maps JSON file and I want to retrieve "++formattedAddress++" from inside it

{
    "statusCode": 200,
    "statusDescription": "OK",
    "copyright": "Copyright © 2013 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
    "authenticationResultCode": "ValidCredentials",
    "resourceSets": [
        {
            "resources": [
                {
                    "__type": "Location:http://schemas.microsoft.com/search/local/ws/rest/v1",
                    "point": {
                        "type": "Point",
                        "coordinates": [
                            63.8185213804245,
                            12.105498909950256
                        ]
                    },
                    "matchCodes": [
                        "Good"
                    ],
                    "address": {
                        "addressLine": "55 Stuff",
                        "locality": "Stuff",
                        "++formattedAddress++": "55 Stuff, 51512 Stuff",
                        "postalCode": "25521",
                        "adminDistrict2": "Stuff-Stuff",
                        "countryRegion": "UK",
                        "adminDistrict": "NL"
                    },
                    "bbox": [
                        84.81465866285382,
                        12.097347537264563,
                        50.822384097995176,
                        7.11365028263595
                    ],
                    "name": "55 Stuff, 51122 Stuff",
                    "confidence": "Medium",
                    "entityType": "Address",
                    "geocodePoints": [
                        {
                            "calculationMethod": "Interpolation",
                            "type": "Point",
                            "usageTypes": [
                                "Display",
                                "Route"
                            ],
                            "coordinates": [
                                50.8185213804245,
                                7.105498909950256
                            ]
                        }
                    ]
                }
            ],
            "estimatedTotal": 1
        }
    ],
    "traceId": "8a13f73cab93472db1253e4c1621c651|BL2M002306|02.00.83.1900|BL2MSNVM001274, BL2MSNVM003152",
    "brandLogoUri": "http://dev.virtualearth.net/Branding/logo_powered_by.png"
}

What I have tried so far is like this:

final JSONArray jsonMainArr = locationData.getJSONArray("resourceSets").getJSONObject(0).getJSONArray("resources");
final JSONObject childJSONObject = jsonMainArr.getJSONObject(0);
return childJSONObject.getString("formattedAddress");

childJSONObject is still 2-3 levels over formattedAddress and the query is becoming highly inefficient

Upvotes: 0

Views: 2900

Answers (2)

Rohit
Rohit

Reputation: 490

There are so much online sites where you paste your complex code and get it in an easy way. e.g. http://json.parser.online.fr/

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

get formattedAddress address value as from current json String :

final JSONObject childJSONObject = jsonMainArr.getJSONObject(0)
                                                .getJSONObject("address");
return childJSONObject.getString("++formattedAddress++");

Upvotes: 4

Related Questions