简体   繁体   中英

Android parsing json file

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.

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.

The JSON string is not correct.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM