简体   繁体   中英

Android: How to parse JSON Array of Array of objects

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"
      }
   ]
]

Thanks

Solved

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);
}

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()).

Thanks chiastic-security also for making me understand.

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);
}

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() ).

try this

String data = ""; //your json data string
JSONArray jarray = new JSONArray(data);

for(int i = 0;i < jarray.length(); i++) {
    JSONArray jarry1 = jarray.getJSONArray(i);
    for(int j = 0; j < jarry1.length(); j++) {
        JSONObject jobj = jarry1.getJSONObject(0);
        String ConditionId = jobj.getString("condition_id");
        String ConditionName = jobj.getString("condition_name");
    }
}
JSONArray jsonarray=new JSONArray(your_data);
                for(int i=0;i<jsonarray.length();i++)
                {
                    JSONArray array=jsonarray.getJSONArray(i);
                    for(int j=0;j<array.length();j++)
                    {
                        JSONObject jsonObject=array.getJSONObject(0);
                        String ConditionId=jsonObject.getString("condition_id");
                        String ConditionName=jsonObject.getString("condition_name");
                    }
                }
      JSONArray jsonArray = new JSONArray("Your Data");
      JSONArray tempArray ; 
      net.sf.json.JSONObject tempJson ;
      for(int i = 0 ; i < jsonArray.length() ; i++)
      {
          tempArray =  jsonArray.getJSONArray(i);

          tempJson =  tempArray.getJSONObject(0);

          tempJson.get("condition_id");

          ....data So On
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM