0

i am trying to fetch one json file in following specific format.

{
   "business":[
      {
     "id":"13",
     "category":"Dinner",
     "subcategory":"",
     "name_eng":"dinner 1",
     "name_arab":"dinner 1",
     "mobile":"12345",
     "address":"not now",
     "logo":"1.gif",
     "contact":"Call",
     "open_time":"10 PM",
     "close_time":"8 PM"
     }
  ],
   "business_cat":[
  [
     "cat 1",
     {
        "name":"dish 1",
        "id":"7",
        "name_arab":"dish1",
        "desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "price":"200 KD",
        "logo":"laptop.jpeg"
     },
     {
        "name":"dish 2",
        "id":"8",
        "name_arab":"dish 2",
        "desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "price":"123789",
        "logo":"micky.jpg"
     },
     {
        "name":"dish3",
        "id":"13",
        "name_arab":"dish3",
        "desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "price":"300 KD",
        "logo":"1.jpg"
     },
     {
        "name":"new dish",
        "id":"15",
        "name_arab":"new dish",
        "desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "price":"213 KD",
        "logo":"12.jpg"
     }
   ],
   [
     "cat2",
     {
        "name":"dish3",
        "id":"14",
        "name_arab":"dish3",
        "desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "price":"123 KD",
        "logo":"10.jpg"
     },
     {
        "name":"dish 4",
        "id":"16",
        "name_arab":"dish4",
        "desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
        "price":"110 KD",
        "logo":"3.jpg"
        }
     ]
   ]
}

now how can i access "business" ,"business_cat" and in "business_cat" "cat 1" and "cat 2" names and in "cat 1" and "cat 2" all the objects of that. please this is somewhat complex for me to handle such json data, help me out.

Hitesh Kamani
  • 935
  • 9
  • 24

2 Answers2

2

Try out as below:

Read the JSON using the Below method:

   public static String parseJSON(String p_url) {
    JSONObject jsonObject = null;
    String json = null;
    try {
        // Create a new HTTP Client
        DefaultHttpClient defaultClient = new DefaultHttpClient();
        // Setup the get request
        HttpGet httpGetRequest = new HttpGet(PeakAboo.EmailUrl + p_url);
        System.out.println("Request URL--->" + PeakAboo.EmailUrl + p_url);
        // Execute the request in the client
        HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
        // Grab the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpResponse.getEntity().getContent(), "UTF-8"));
        json = reader.readLine();
        System.err.println("JSON Response--->" + json);
        // Instantiate a JSON object from the request response
        jsonObject = new JSONObject(json);
    } catch (Exception e) {
        // In your production code handle any errors and catch the
        // individual exceptions
        e.printStackTrace();
    }
    return json;
}

After that parse it as below:

      try{
          String response=parseJSON(URL);
        JSONObject responseJson = new JSONObject(response);
        JSONArray m_bus_cat=responseJson.getJSONArray("business_cat");
            for(int i=0; i<m_bus_cat.length(); i++){
                     JSONObject catJson = new JSONObject(i);
                      if(catJson .has("cat 1")){
                       JSONArray resultArr = resultInstanceJson.getJSONArray("cat 1");
                    for(int j=0; j<resultArr.length(); j++){
                        String Name = catJson .getString("name");
                       String id=catJson.getString("id");
                    }
                 }
                }
            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
GrIsHu
  • 28,867
  • 10
  • 63
  • 100
0

try following code to parse response data

here jsonString is string you are getting from response.

try {
        JSONObject jsonObject=new JSONObject(jsonString);
        JSONArray business=jsonObject.getJSONArray("business");
        for(int i=0;i<business.length();i++){
            JSONObject businessObject=business.getJSONObject(i);
            //you can store in array or arraylist
            String id=businessObject.getString("id");
            String category=businessObject.getString("category");
            // and so on
        }

        JSONArray businessCat=jsonObject.getJSONArray("business_cat");
        for(int i=0;i<businessCat.length();i++){
            JSONArray businessCatInner=businessCat.getJSONArray(i);
            String cat=businessCatInner.getString(0);

            for(int j=0;j<businessCatInner.length();j++){
                JSONObject businessInnerObject=businessCatInner.getJSONObject(i);
                //you can store in array or arraylist
                String name=businessInnerObject.getString("name");
                String name_arab=businessInnerObject.getString("name_arab");
                // and so on    
            }


        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Ketan Ahir
  • 6,608
  • 1
  • 21
  • 43