3

Following is My Json File:-

"Restaurants":{

    "8":{
        "Res_name":"Purple Cafe and Wine Bar",
        "foodtype":"American, Wine",
        "city":"Seattle",
        "state":"WA",
        "latitude":"0",
        "longitude":"0"
    },
    "9":{
        "Res_name":"Quinn's",
        "foodtype":"American, Pubs",
        "city":"Seattle",
        "state":"WA",
        "latitude":"0",
        "longitude":"0"
    },
    "19":{
        "Res_name":"Dahlia Lounge",
        "foodtype":"American",
        "city":"Seattle",
        "state":"WA",
        "latitude":"0",
        "longitude":"0"
    },
},

I am Using below code for json parsing:-

try {
    JSONObject jsonObj = new JSONObject(res);
    JSONObject mRestaurant = jsonObj.getJSONObject("Restaurants");
    String mResult = jsonObj.getString("Result");
    System.out.println("mRestaurant is:- " + mRestaurant);
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

The mRestaurant Value is below:-

{"487":{"state":"WA","Res_name":"SAM Taste","longitude":"0","latitude":"0","foodtype":"American","city":"Seattle"},"332":{"state":"WA","Res_name":"Luna Park Cafe","longitude":"0","latitude":"0","foodtype":"American","city":"Seattle"},"35":{"state":"WA","Res_name":"Restaurant Zoe","longitude":"0","latitude":"0","foodtype":"American, Bar","city":"Seattle"},"

but what is the next step for getting Res_Name, foodtype from above response.

Any Help would be appreciated.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128

3 Answers3

3

The below code is next step for json parsing.

public void getdata() {
    String res = mWebRequest.performGet(Constants.url+ "restaurants.php? action=searchRestaurant&lat=0&lon=0&foodtype="+ mEdttxtSearch.getText().toString() + "&state="+ mEdttxtSearch.getText().toString() + "&city="+                                                                             mEdttxtSearch.getText().toString()+ "&devType=Android");
    System.out.println("res is:- " + res);
    if (res != null) {
        try {
            JSONObject jsonObj = new JSONObject(res);
            JSONObject mRestaurants = jsonObj.getJSONObject("Restaurants");
            String mResult = jsonObj.getString("Result");
            if (jsonObj.has("Restaurants")) {
                Iterator<Object> keys = mRestaurants.keys();
                while (keys.hasNext()) {
                    String key = (String) keys.next();
                    JSONObject obj = new JSONObject();
                    obj = mRestaurants.getJSONObject(key);
                    mRes_Name.add(obj.getString("Res_name"));
                    mLatitude.add(obj.getString("latitude"));
                    mLongitude.add(obj.getString("longitude"));
                    mState.add(obj.getString("state"));
                    mCity.add(obj.getString("city"));
                    mFood_Type.add(obj.getString("foodtype"));
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
2

Use the get() method:

String mRestaurant = jsonObj.get("487").get("Res_name");
lucapette
  • 20,564
  • 6
  • 65
  • 59
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    It's an example; figure it out. Sorry, you neglected to tell what size spoon you needed to be fed with. – duffymo Jan 13 '12 at 12:45
0

use gson for the same, as it supports direct conversion from json to java and java to json, please see following link:

Converting JSON to Java

Community
  • 1
  • 1
jeet
  • 29,001
  • 6
  • 52
  • 53