You can use this code to parse the JSONObject:
private void parseDishData(JSONObject jsonObj){
try{
int dishId = jsonObj.optInt("dish_id", -1);
String disSizes = jsonObj.optString("dish_size", "NA");
String price = jsonObj.optString("price", "NA");
Log.e("MyDishData", "dishId = " + dishId + " disSizes = " + disSizes)
}
catch(Exception ex){
Log.e("ERROR", ex.getMessage());
}
}
Or if you have a String
you can use this code to parse the JSON String:
private void parseDishData(String json){
try{
JSONObject jsonObj = new JSONObject(json);
int dishId = jsonObj.optInt("dish_id", -1);
String disSizes = jsonObj.optString("dish_size", "NA");
String price = jsonObj.optString("price", "false");
Log.e("MyDishData", "dishId = " + dishId + " disSizes = " + disSizes)
}
catch(Exception ex){
Log.e("ERROR", ex.getMessage());
}
}
Just pass the parseDishData
method the JSON string.
EDIT:
Your error:
E/ERROR: Expected literal value at character 73 of {id=830,
dish_id=789, dish_size=Small, price=21, weight=350, promo_price=,
dishes_num_for_promo=0
You are getting the above error because there appears to be a problem with the JSON string. The key " promo_price" has no value! "promo_price=,". There should be a value for example: "promo_price=10,".