本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何在android中获取json值

发布于2023-09-25 20:20     阅读(323)     评论(0)     点赞(12)     收藏(0)


I read many example and try it. But it didn't work to get the value.

The result returned a whole JSON to me.

I don't know where is wrong...

This is my first time to ask question. Thanks for help.

Here is my JSON and JAVA Code.

JSON:`

{
    "food":{
        "name":"XXX Food",
        "details":{
            "code":"01",
            "location":{
                "area":"Area D",
                "place":"Food"
            },
            "price":"132.21",
            "discount":"15%"
        }
    }
}

Now, I want to get the price.

JAVA Code:

JSONObject json = new JSONObject(recvStr);
            JSONArray Results = json.getJSONArray("food");
            JSONArray msg = Results.getJSONArray(0);

            result = msg.getJSONObject(0).getString("price");

解决方案


Results is not a JSONArray but a JSONObject:

JSONObject results = json.getJSONObject("food");
String name = results.getString("name");
JSONObject details = results.getJSONObject("details");
String price = details.getString("price");

You can read more about JSON at json.org.



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/677407/16b6d4285763e2755180/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

12 0
收藏该文
已收藏

评论内容:(最多支持255个字符)