我想从ApiVk方法response.responseString获取数据
String jsonStr1 =response.responseString;
JSONArray arr = new JSONArray(jsonStr1);
JSONObject jObj = arr.getJSONObject(0);
String date = jObj.getString("id");
我得到了 NPE 如何解析字符串以获得值“id”的数组
String jsonStr1 = "{\"response\":{\"count\":254,\"items\":[{\"last_name\":\"Екимов\",\"id\":325813465,\"first_name\":\"Артём\"},{\"last_name\":\"Виноградов\",\"id\":448209461,\"first_name\":\"Дэнчик\"},{\"last_name\":\"Κонстантинов\",\"id\":444441827,\"first_name\":\"Κонстантин\"}]}}";
最佳答案
对于这个 json:
{
"response": {
"count": 254,
"items": [{
"last_name": "Екимов",
"id": 23892340,
"first_name": "Артём"
}, {
"last_name": "Екимов",
"id": 23892381,
"first_name": "Артём"
}, {
"last_name": "Екимов",
"id": 23828492,
"first_name": "Артём"
}]
}
}
获取id的分析如下:
try {
JSONObject jsonresponse = jsonResponse.getJSONObject("response");
JSONArray getJsonArray = jsonresponse.getJSONArray("items");
for(int i = 0; i<getJsonArray.length();i++){
JSONObject jsonAux= (JSONObject) jsonArrayItems.get(i);
String id = jsonAux.getString("id");
Log.e("ID = ",id);
}
} catch (JSONException e) {
e.printStackTrace();
}
希望对您有所帮助。
关于java - Android 从 Json 对象解析字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467642/