gpt4 book ai didi

java - 当我从服务器获取 JSON 字符串时,JSONArray 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:31 27 4
gpt4 key购买 nike

我已经查找了一些答案,但不确定为什么我的答案完全失败...

代码看起来像这样

        HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String json = EntityUtils.toString(httpEntity);

//Convert to JsonArray
JSONArray jsonArray = new JSONArray(json);

Log.i(DEBUG_TAG, Integer.toString(jsonArray.length()));

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(DEBUG_TAG, jsonObject.getString(KEY_ID));

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, jsonObject.getString(KEY_ID));
map.put(KEY_TITLE, jsonObject.getString(KEY_TITLE));
map.put(KEY_ARTIST, jsonObject.getString(KEY_ARTIST));
map.put(KEY_DURATION, jsonObject.getString(KEY_DURATION));
map.put(KEY_VOTECOUNT, jsonObject.getString(KEY_VOTECOUNT));
map.put(KEY_THUMB_URL, jsonObject.getString(KEY_THUMB_URL));
map.put(KEY_GENRE, jsonObject.getString(KEY_GENRE));

//Adding map to ArrayList
if (Integer.parseInt(jsonObject.getString(KEY_VOTECOUNT)) == -1){
//If VoteCount is -1 then add to header
headerList.add(map);
}else {
songsList.add(map);
}
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

当我在 String json 上运行 logcat 时,它似乎显示了正确的信息,有点像这样......

  {
"userdata": [
{
"id": "8",
"title": "Baby One More Time",
"artist": "Britney Spears",
"duration": "03:24:00",
"votes": "0",
"thumb_url": "http://api.androidhive.info/music/images/dido.png",
"genre": null
},
{
"id": "2",
"title": "As Long As You Love Me",
"artist": "Justin Bieber",
"duration": "05:26:00",
"votes": "0",
"thumb_url": "http://api.androidhive.info/music/images/enrique.png",
"genre": "Rock"
}
]
}

和 logcat 上的

JSONArray jsonArray = new JSONArray(json);

告诉我 jsonArray.length()

10-31 22:57:28.433: W/CustomizedListView(26945): error! Invalid index 0, size is 0

请告诉我

谢谢,

最佳答案

问题是它不是 JSON 数组。这是一个 JSON 对象,JSON 数组以 [ 开始并以 ] 结束

enter image description here

JSON 对象以 { 开头,以 } 结尾

enter image description here

如需进一步引用,您可以在此处查看 -> http://www.json.org/

要修复它,您应该先将 json 字符串转换为 json 对象,然后解析 json 对象以获取 json 数组

这是一个从jsonobject解析json数组的例子

void ParseAPIWithJSON()
{
String readGooglePlace = readGooglePlaceAPI();
try
{

InputStream is = new ByteArrayInputStream(readTwitterFeed.getBytes("UTF-8"));
byte [] buffer = new byte[is.available()];
while (is.read(buffer) != -1);
String jsontext = new String(buffer);
JSONObject entries = new JSONObject(jsontext);

JSONArray hasil = entries.getJSONArray("results");
results = hasil.getString(o);
Log.i("TAG", results);
int i;
Log.i("TAG", Integer.toString(hasil.length()));
numberofPlaces = hasil.length();
for (i=0;i<hasil.length();i++)
{
JSONObject data = hasil.getJSONObject(i);
namePlaces[i] = data.getString("name");
Log.i("TAG", namePlaces[i]);
JSONObject geometry = data.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
latPlaces[i] = location.getDouble("lat");
longPlaces[i] = location.getDouble("lng");
Log.i("TAG", "Lat : "+latPlaces[i]+" Long : "+longPlaces[i]);
}

}
catch (Exception je)
{
Log.e("TEST1", je.getMessage());
}
}

从整个代码来看我认为你只需要理解这一点

 String jsontext = new String(buffer);
JSONObject entries = new JSONObject(jsontext);
JSONArray hasil = entries.getJSONArray("results");

转换字符串->jsonobject->jsonarray->获取值

关于java - 当我从服务器获取 JSON 字符串时,JSONArray 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13170333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com