Android:如何解析JSON Array of Object Array [英] Android: How to parse JSON Array of Array of objects

查看:154
本文介绍了Android:如何解析JSON Array of Object Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我如何在android中解析这种json吗?

Can any body tell me how I parse this type of json in android?

[
   [
      {
         "condition_id":"1",
         "condition_name":"Type 1 Diebetics"
      }
   ],
   [
      {
         "condition_id":"2",
         "condition_name":"Type 2 dypatise"
      }
   ]
]

谢谢

已解决

非常感谢您nayoso 它对我有用.

Thank you very much nayoso Its worked for me.

String jsonString = "[
   [
      {
         "condition_id":"1",
         "condition_name":"Type 1 Diebetics"
      }
   ],
   [
      {
         "condition_id":"2",
         "condition_name":"Type 2 dypatise"
      }
   ]
]";
JSONArray jsonArray = new JSONArray(jsonString);
for(int i=0;i<jsonArray.length(),i++) {
   JSONArray childJsonArray = jsonArray.getJSONArray(i);
   JSONObject contentJsonObject = childJsonArray.getJSONObject(0);
   String conditionID = contentJsonObject.getString('condition_id');
   String conditionName = contentJsonObject.getString('condition_name');
   Log.i("TAG","Index "+i+" condition_id "+conditionID+" condition_name "+conditionName);
}

您可以使用org.json库轻松完成此操作.整个事情是一个JSONArray;在位置0(使用.get(0))处,您还有另一个JSONArray;在该位置的0处,您有一个JSONObject,该对象将键映射到值(使用.getString()).

You can do this easily with the org.json library. The whole thing is a JSONArray; at position 0 (use .get(0)) you have another JSONArray; at position 0 of that, you have a JSONObject, which maps keys to values (use .getString()).

也感谢chiastic-security让我理解.

推荐答案

您可以使用内置的JSONArray和JSONObject类

You can use built in JSONArray and JSONObject classes

String jsonString = "[
   [
      {
         "condition_id":"1",
         "condition_name":"Type 1 Diebetics"
      }
   ],
   [
      {
         "condition_id":"2",
         "condition_name":"Type 2 dypatise"
      }
   ]
]";
JSONArray jsonArray = new JSONArray(jsonString);
for(int i=0;i<jsonArray.length(),i++) {
   JSONArray childJsonArray = jsonArray.getJSONArray(i);
   JSONObject contentJsonObject = childJsonArray.getJSONObject(0);
   String conditionID = contentJsonObject.getString('condition_id');
   String conditionName = contentJsonObject.getString('condition_name');
   Log.i("TAG","Index "+i+" condition_id "+conditionID+" condition_name "+conditionName);
}

这篇关于Android:如何解析JSON Array of Object Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆