1

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.

3
  • This pretty much explains it. Commented Sep 30, 2014 at 9:32
  • Thanks for your Response I read it out. Commented Sep 30, 2014 at 9:34
  • why given me down vote on that question? Commented Sep 30, 2014 at 9:58

5 Answers 5

2

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
also FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground() and Caused by: java.lang.NoSuchMethodError: org.json.JSONArray.<init>
1

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

Comments

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

Comments

1
      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
      }

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.