3

我的 JSON 看起来像这样-

{"ipinfo": {
    "ip_address":"4.2.2.2",
     "ip_type":"Mapped",

                "Location":{

                            "continent":"north america",

                            "latitude":33.499,

                            "longitude":-117.662,

                        "CountryData":{

                                "country":"united states",

                                "country_code":"us"},

                        "region":"southwest",

                        "StateData":{

                                "state":"california",

                                "state_code":"ca"},

                        "CityData":{

                                "city":"san juan capistrano",

                                "postal_code":"92675",

                                "time_zone":-8}}

    }}

这是我下面的代码,它试图访问 JSONArray 中的项目成员

    try {
        String url = service + version + method + ipAddress + format;
        StringBuilder builder = new StringBuilder();
        httpclient = new DefaultHttpClient();
        httpget = new HttpGet(url);
        httpget.getRequestLine();
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = entity.getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            for (String line = null; (line = bufferedReader.readLine()) != null;) {
                builder.append(line).append("\n");
            }
            //Exception getting thrown in below line
            JSONArray jsonArray = new JSONArray(builder.toString());
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
            }
        }

    } catch (Exception e) {
        getLogger().log(LogLevel.ERROR, e.getMessage());
    } finally {
        bufferedReader.close();
        httpclient.getConnectionManager().shutdown();
    }

我总是在这一行抛出异常-

JSONArray jsonArray = new JSONArray(builder.toString());

下面是抛出异常

org.json.JSONException: A JSONArray text must start with '[' at character 1

谁能建议我在我的代码中做错了什么?我该如何改进它?

4

2 回答 2

5

我没有使用那个特定的 API,但是从对象被命名的事实来看JSONArray(关键字:数组),我猜它需要一个数组。使用 JSON,数组必须以 a 开头并以[结尾]

[1, 2, 3, 4]

它可以包含对象:

[{}, {}, {}]

请注意对象如何以 开头{和结尾},这与数组不同:

{
    "name": "My Object!"
}

由于您的 JSON 数据看起来更像一个{object}而不是一个[array],因此您应该尝试使用它JSONObject

实际上,尽管您有两个选择:您可以将 JSON 数据更改为数组,或者您可以将 Java 代码更改为使用JSONObject. (一个或另一个;不是两者。)

更改 JSON 数据

就像[在开头和]结尾添加 a 一样简单:

[
    {
        "ipinfo": {
            "ip_address": "4.2.2.2",
            "ip_type": "Mapped",
            "Location": {
                "continent": "north america",
                "latitude": 33.499,
                "longitude": -117.662,
                "CountryData": {
                    "country": "united states",
                    "country_code": "us"
                },
                "region": "southwest",
                "StateData": {
                    "state": "california",
                    "state_code": "ca"
                },
                "CityData": {
                    "city": "san juan capistrano",
                    "postal_code": "92675",
                    "time_zone": -8
                }
            }
        }
    }
]

更改 Java

最终的 Java 看起来有点像:

// OLD CODE
//JSONArray jsonArray = new JSONArray(builder.toString());
//for (int i = 0; i < jsonArray.length(); i++) {
//    JSONObject jsonObject = jsonArray.getJSONObject(i);
//}
// END OLD CODE
JSONObject jsonObject = new JSONObject(builder.toString());

(再次,一个或另一个;不是两者。)

于 2012-10-13T05:16:38.030 回答
2

您的源 JSON 只是一个对象。与其加载到数组中,不如直接加载到 a 中JSONObject就足够了。

JSONObject jsonObject = new JSONObject(builder.toString());

该对象将有一个名为 的属性ipinfo

于 2012-10-13T05:15:43.997 回答