0

Json string contained in String str:

{conf:{"quantity_uom":"l",price_uom:"euro",distance_uom:"km",consumption_uom:"km/l"}}

Code:

try{
        if (str!=""){
            this.json = new JSONObject(str);
            this.quantityUom=this.json.getString("quantity_uom");
            this.distanceUom=this.json.getString("distance_uom");
            this.priceUom=this.json.getString("price_uom");
            this.consumptionUom=this.json.getString("consumption_uom");             
        }
    }catch (Exception e) {
        throw e;
    }

I have returned No value for quantity_uom. How I am doing wrong? String contain the json text.

Thank you so much.

2
  • str!="" will not work Java/Android. Use this: !str.equals("") Commented Oct 6, 2011 at 10:52
  • Your string is not valid JSON. Commented Oct 6, 2011 at 12:27

2 Answers 2

4
try{
    if (!str.equals("")){
        Json obj = new JSONObject(str);
        this.json = obj.getJSONObject("conf");
        this.quantityUom=this.json.getString("quantity_uom");
        this.distanceUom=this.json.getString("distance_uom");
        this.priceUom=this.json.getString("price_uom");
        this.consumptionUom=this.json.getString("consumption_uom");             
    }
}catch (Exception e) {
    throw e;
}

You first have to get json object named 'conf' from string and from that json object get other values.

Sign up to request clarification or add additional context in comments.

Comments

1

The JSON string is not correct.

Try this:

{"conf":{"quantity_uom":"l","price_uom":"euro","distance_uom":"km","consumption_uom":"km/l"}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.