我以下面的格式嵌套了json数组。我正在使用volley liabrary进行JSON解析。
B
下面是我在android中编写的代码。
{
"City": [{
"name": "Mumbai",
"Mumbai": [{
"area": "andheri",
"diler": [{
"DName": "yuvraj"
}]
}, {
"area": "jogeshwari"
}, {
"area": "goregaon"
}]
},
{
"name": "Nashik",
"Nashik": [{
"area": "clg rd",
"diler": [{
"DName": "yuvraj"
}]
}, {
"area": "GP RD",
"diler": [{
"DName": "Roshan"
}]
}, {
"area": "CBS",
"diler": [{
"DName": "Deepak"
}]
}]
}, {
"name": "Bengaluru"
}
]}
但是我在Android中解析时遇到了错误。 错误是 - JSONObject无法转换为JSONArray
任何人都可以提供示例代码来解析这个json数组。
先谢谢
答案 0 :(得分:4)
以下是您的问题解决方案,请使用以下代码
try {
JSONObject jsonObject = new JSONObject("response");
JSONArray jsonArray = jsonObject.getJSONArray("City");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String name = object.getString("name");
if (object.length() != 0) {
Iterator<String> key = object.keys();
while (key.hasNext()) {
String cityname = key.next();
JSONArray ja = object.getJSONArray(cityname);
for (int j = 0; j < ja.length(); j++) {
JSONObject object1 = ja.getJSONObject(j);
String area = object1.getString("area");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
并尝试使您的JSON格式相同。
答案 1 :(得分:3)
关键是第一个json节点是JsonObject, 以这种方式更改代码并继续解析:
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
答案 2 :(得分:1)
使用JSONObject解析数据,如:
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("City");
for(int i = 0; i<jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray .getJSONObject(i);
String name = jsonObject.getString("name");
JSONArray city_array = jsonObject.getJSONArray(name);
for(int j=0; j<city_array.length(); j++)
{
JSONObject obj = city_array.getJSONObject(j);
String area = obj.getString("area");
JSONArray diler_array = obj.getJSONArray("diler");
JSONObject obj1 = diler_array.getJSONObject(0);
String DName = obj1.getString("DName");
}
}
请检查已修改的答案。
答案 3 :(得分:0)
使用volley
在android中的数组内传递数据数组public List<HashMap<String,String>> returnJsonforPhp(){
HashMap<String,String> map = new HashMap<String,String>();
List<HashMap<String,String>> forjson= new ArrayList<HashMap<String,String>>();
/*for(int i=0;i<dblist;i++) {
map.put("name",dblist.getName());
map.put("email",dblist.getEmail());
map.put("mobno",dblist.getMobno());
forjson.add(map);
}*/
map.put("name","ravi");
map.put("email","ravi@gmail.com");
map.put("mobno","9897939595");
forjson.add(map);
return forjson;
}
字符串OTP_Url =&#34; https://www.yourdomain.com/rest/updateregistration/&#34 ;;
public String getJson()抛出JsonProcessingException {
列表&gt; list = new ArrayList&gt;(); 列表&gt;&GT; newlist = new ArrayList&gt;&gt;(); newlist.add(列表);
HashMap<String,String> map = new HashMap<String, String>();
map.put("name","knnkl");
map.put("email","kjbjbk");
map.put("password","njnjknk");
list.add(map);
newlist.add(list);
String parsedJson = new ObjectMapper().writeValueAsString(newlist);//json conversion
return parsedJson;
}
编译( [group:&#39; com.fasterxml.jackson.core&#39;,name:&#39; jackson-core&#39;,version:&#39; 2.4.1&#39;], [group:&#39; com.fasterxml.jackson.core&#39;,name:&#39; jackson-annotations&#39;,version:&#39; 2.4.1&#39;], [group:&#39; com.fasterxml.jackson.core&#39;,name:&#39; jackson-databind&#39;,version:&#39; 2.4.1&#39;] )