Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
This pretty much explains it. –  SME_Dev Sep 30 '14 at 9:32
    
Thanks for your Response I read it out. –  Revolution Sep 30 '14 at 9:34
    
why given me down vote on that question? –  Revolution Sep 30 '14 at 9:58

5 Answers 5

up vote 2 down vote accepted

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);
}
share|improve this answer
    
I am getting this exception org.json.JSONException: Value [{"condition_id":"2","condition_name":"Type 2 dypatise"}] at 1 of type org.json.JSONArray cannot be converted to JSONObject –  Revolution Sep 30 '14 at 9:45
    
also FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground() and Caused by: java.lang.NoSuchMethodError: org.json.JSONArray.<init> –  Revolution Sep 30 '14 at 9:49
    
Its solved, I get the values Thank you very much, –  Revolution Sep 30 '14 at 9:53

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

share|improve this answer

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");
    }
}
share|improve this answer
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");
                    }
                }
share|improve this answer
      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
      }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.