[英]How to parse JsonArray values in Android
I would like to parse all name and id attribute from the following JSon Array: JSon link 我想从以下JSon数组解析所有名称和id属性: JSon链接
I tried with the following code but not working. 我尝试使用以下代码,但无法正常工作。 I get
我懂了
org.json.JSONException: Value [{"id":0,"name":"Alsópetény"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
Exception 例外
My code : 我的代码:
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+"");
}
Yours (as I can see from the link you've posted) is a single JSON message {...} wich contains many nodes ("A","B", "E", "F"... and so on). 您的(从您发布的链接中可以看到)是一条JSON消息{...},其中包含许多节点(“ A”,“ B”,“ E”,“ F” ...等) )。 Every node is a JSON Array wich contains some JSON Objects.
每个节点都是一个JSON数组,其中包含一些JSON对象。
You must Get first the JSON Objects: "A", "B",... this way: 您必须首先通过以下方式获取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");
...
Then you must access to the JsonArray elements: 然后,您必须访问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 + "");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.