0

次の特定の形式で1つのjsonファイルを取得しようとしています。

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

「business」、「business_cat」、および「business_cat」の「cat 1」と「cat 2」の名前、および「cat 1」と「cat 2」のすべてのオブジェクトにアクセスするにはどうすればよいですか。このようなjsonデータを処理するのはやや複雑です。助けてください。

4

2 に答える 2

2

以下のようにしてみてください。

以下のメソッドを使用して JSON を読み取ります。

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

その後、以下のように解析します。

      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();
    }
于 2013-09-13T04:43:06.210 に答える
0

次のコードを試して、応答データを解析してください

ここjsonStringに、応答から取得する文字列があります。

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();
    }
于 2013-09-13T05:20:02.267 に答える