我的 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
谁能建议我在我的代码中做错了什么?我该如何改进它?