Android解析json文件

发布于 2024-12-08 04:21:02 字数 690 浏览 5 评论 0原文

String str 中包含的 Json 字符串:

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

代码:

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;
    }

我已返回 No value for amount_uom。我怎么做错了?字符串包含 json 文本。

太感谢了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨洒年华 2024-12-15 04:21:02
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;
}

您首先必须从字符串中获取名为“conf”的 json 对象,然后从该 json 对象中获取其他值。

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.

小镇女孩 2024-12-15 04:21:02

JSON 字符串不正确。

试试这个:

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

The JSON string is not correct.

Try this:

{"conf":{"quantity_uom":"l","price_uom":"euro","distance_uom":"km","consumption_uom":"km/l"}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文