如何在Android中解析JsonArray值
我想解析以下JSon数组中的所有name和id属性: 我尝试了以下代码,但不起作用。我明白了如何在Android中解析JsonArray值,android,arrays,json,Android,Arrays,Json,我想解析以下JSon数组中的所有name和id属性: 我尝试了以下代码,但不起作用。我明白了 org.json.JSONException: Value [{"id":0,"name":"Alsópetény"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject 例外情况 我的代码: JSONArray jsonarray = new JSONArray(jsonList);
org.json.JSONException: Value [{"id":0,"name":"Alsópetény"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
例外情况
我的代码:
JSONArray jsonarray = new JSONArray(jsonList);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String id= jsonobject.getString("id");
String name= jsonobject.getString("name");
System.out.print("ID:\n"+id+"");
System.out.print("NAME:\n"+name+"");
}
JSONArray-JSONArray=新的JSONArray(jsonList);
for(int i=0;i
您的(正如我从您发布的链接中看到的)是一条JSON消息{…},其中包含许多节点(“a”、“B”、“E”、“F”…等等)。每个节点都是一个JSON数组,其中包含一些JSON对象
您必须首先获得JSON对象:“A”、“B”,。。。这样:
String result = EntityUtils.toString(response.getEntity()); //the result of HTTP call
JSONObject JO = new JSONObject(result);
JSONArray ja = JO.getJSONArray("A");
JSONArray jb = JO.getJSONArray("B");
...
然后必须访问JsonArray元素:
for(int i=0; i< ja.length(); i++){
JSONObject jo = ja.getJSONObject(i);
String id = jo.getString("id");
String name = jo.getString("name");
System.out.println("id: " + id + "");
System.out.println("name: " + name + "");
}
for(int i=0;i
我使用jsonList=“[{\'id\':0,\'name\':\'Alsópetény\'}]”运行了您的代码。
没有产生任何错误。。。