[英]getJSONObject returns wrong value
我正在使用返回JSON的REST Web服務。 但是JSON Object的值是
“ [12]”
當我打印。 值只能是12
不帶"[]"
我的代碼錯了嗎? 有我的代碼:
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://applocalize.com.br/rest/rest.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("pg","categorias"));
nameValuePairs.add(new BasicNameValuePair("serv","buscar"));
nameValuePairs.add(new BasicNameValuePair("dt_atualizacao","0"));
try{
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}catch (UnsupportedEncodingException e){
}
HttpResponse resp = httpClient.execute(post);
String respStr = EntityUtils.toString(resp.getEntity());
if (resp.getStatusLine().getStatusCode() == 200)
{
System.out.println(respStr);
JSONArray temp1 = new JSONArray(respStr);
for (int i=0; i < temp1.length(); i++) {
JSONObject obj = temp1.getJSONObject(i);
System.out.println(obj.getString("id"));
}
}
System.out.println("OKAY!");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
我在打印id時有我的日志貓
02-20 13:45:32.560 31135-31135/com.localizeapp I/System.out﹕ ["52"]
02-20 13:45:32.560 31135-31135/com.localizeapp I/System.out﹕ ["53"]
02-20 13:45:32.560 31135-31135/com.localizeapp I/System.out﹕ ["54"]
編輯:
respStr:
02-20 13:45:32.210 31135-31135 / com.allapps.localizeapp I / System.out:[{“ id”:[“ 51”],“ titulo”:[“ Chaveiro”],“ icone”:[ “這里的基數為64。......太長了”]
問題是您的Web服務將值返回為JSONArray
。 您可以更改Web服務以將數據發送為
"id":"51"
或者你可以替換
System.out.println(obj.getString("id"));
與
System.out.println(obj.getJSONArray("id").get(0));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.