从JSONObject内部获取值

时间:2013-01-14 12:34:19

标签: java android json

我有这个Bing Maps JSON文件,我想从里面检索“++ formattedAddress ++”

{
    "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"
}

到目前为止我尝试的是这样的:

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

childJSONObject仍然是格式化地址的2-3级,并且查询变得非常低效

2 个答案:

答案 0 :(得分:3)

从当前json获取formattedAddressAddress地址值String:

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

答案 1 :(得分:0)

有很多在线网站可以粘贴复杂的代码并轻松获取。例如http://json.parser.online.fr/