0

I have this code and System.out.println(tempObj); print this json(I/System.out: {id=830, dish_id=789, dish_size=Small, price=21, weight=350, promo_price=, dishes_num_for_promo=0}), can you please help me how to access these json value in JAVA Android

for(int i=0; i<disSizes.length; i++ ){
        Object tempObj =  disSizes[i];

        System.out.println(disSizes);
        System.out.println(tempObj);

    }
Umashankar Saw
  • 1,131
  • 1
  • 9
  • 25

3 Answers3

1

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,".

Barns
  • 4,850
  • 3
  • 17
  • 31
  • Getting this 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} – Umashankar Saw Jul 08 '18 at 18:23
  • tempObj varibale is java.lang.Object – Umashankar Saw Jul 08 '18 at 18:23
  • @UmashankarSaw :: There appears to be an issue with the JSON string. The key " promo_price" has no value! "promo_price=," – Barns Jul 08 '18 at 18:45
  • @UmashankarSaw :: Have you had a chance to look at the explanation of why you are getting the error. If you need any help with that, please let me know. – Barns Jul 10 '18 at 15:09
0

You can construct a JSONObject by casting the object:

JSONObject json = (JSONObject) tempObj;

Now you can access the properties by using the appropriate getters from the JSONObject class according to the type of data you want to get out of it.

For instance you do:

int id = json.getInt("id");

to obtain the id of the object.

NiVeR
  • 9,644
  • 4
  • 30
  • 35
0

You will cast Object to JSONObject to extract values from this Json.

JSONObject json = (JSONObject) tempObj;

Then you can parse Json by simple methods like

String promo_price = json.getString("promo_price");
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212