私の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
誰かが私のコードで何が間違っているのか教えてもらえますか?そして、どうすればそれを改善できますか?