I am trying to parse more than one json array which consists of nested json arrys. I have googled a lot but couldn't understand exactly what to do. I have done the following code but it doesn't give me all the values.
I have done this coding so far and the result
string displays items array
and the jArray
displays questions array
. I am totally new to json and hence please help me step by step.
String result = null;
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("url");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success "+"nameValuePairs");
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.e("log_tag", "result "+result.toString());
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}
try
{
JSONArray jArray=new JSONArray(result);
Log.w("Lengh",""+jArray);
String s="",s1,s2,s3,s4,s5,s6,s7,s8,s9;
Log.w("Lengh",""+jArray.length());
for(int i=0;i<jArray.length();i++){
JSONArray newarr = jArray.getJSONObject(i).getJSONArray("items");
Log.w("Lengh",""+newarr.length());
for(int j= 0; j<newarr.length();j++){
JSONObject json_data = newarr.getJSONObject(i);
s=json_data.getString("id");
Log.w("parsed data",""+s);
// System.out.println("Id is: "+ s);
}
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
Json Array
[
{
"items": [
{
"id": "11",
"Item_Id": "123",
"Item_Name": "Chicken Cream Soup",
"Price": "8",
"Currency": "AED",
"Category": "Soup",
"Description": "Creamy Chicken Soup with garnish & side helpings",
"Unit": "2",
"food_type": "Non",
"Image_Large": "/images_large/chickensoup.jpg",
"Image_Thumb": "/images_large/chickensoup.jpg",
"Timestamp": "6/23/2014 9:49:43 PM",
"Promotion": "",
"Item_Name_arabic": "حساء الطماطم",
"Item_Name_russian": "",
"Currency_arabic": "درهم",
"Currency_russian": "",
"Description_arabic": "حساء الطماطم",
"Description_russian": "",
"Note": "",
"Nutritional_info": "",
"extrafield_1": "",
"extrafield_2": "",
"preferncess": [
"No Salt",
"Extra Sugar"
],
"preferncess_ids": [
"1",
"2"
],
"price": [
"4",
"5"
],
"preferncess_arabic": [
"لا الملح",
"سكر اضافية"
]
},
{
"id": "12",
"Item_Id": "501",
"Item_Name": "Pasta Napolitan",
"Price": "18",
"Currency": "AED",
"Category": "Pasta",
"Description": "Pasta in Napolitan Sauce",
"Unit": "20",
"food_type": "Non",
"Image_Large": "/images_large/pasta.jpg",
"Image_Thumb": "/images_large/pasta.jpg",
"Timestamp": "6/23/2014 9:47:45 PM",
"Promotion": "",
"Item_Name_arabic": "حساء الطماطم",
"Item_Name_russian": "",
"Currency_arabic": "درهم",
"Currency_russian": "",
"Description_arabic": "حساء الطماطم",
"Description_russian": "",
"Note": "",
"Nutritional_info": "",
"extrafield_1": "",
"extrafield_2": "",
"preferncess": [
"No Salt"
],
"preferncess_ids": [
"3"
],
"price": [
"5"
],
"preferncess_arabic": [
"لا الملح"
]
},
/* values till id =25 are present */
],
"categories": [
{
"categoryName": "Salads",
"categoryShortName": "Salads",
"catid": "0",
"categoryArabicName": "سلطة خضراء"
},
{
"categoryName": "Mezzah",
"categoryShortName": "Mezzah",
"catid": "1",
"categoryArabicName": "المزة"
},
/* some more values present */
"questions": [
{
"q_id": "1",
"q_question": "How would you rate our Menu ?",
"q_option1": "Excellent",
"q_option2": "Very Good",
"q_option3": "Good",
"q_option4": "Bad",
"q_option5": "Terrible",
"Timestamp": "9/12/2013 3:31:55 PM",
"q_status": "1"
},
{
"q_id": "2",
"q_question": "How would you rate our presentation, taste and quality of food ?",
"q_option1": "Excellent",
"q_option2": "Very Good",
"q_option3": "Good",
"q_option4": "Bad",
"q_option5": "Terrible",
"Timestamp": "9/12/2013 3:31:55 PM",
"q_status": "1"
},
/* some more values present */
}
]