【问题标题】:How do i acess multilevel json value from this json ?fr eg:in this json access menu from dish_description [duplicate]我如何从这个 json 访问多级 json 值?例如:在这个 json 访问菜单中的菜描述 [重复]
【发布时间】:2015-05-22 10:31:15
【问题描述】:
   [
  {
    "vendor_name": "Chef Neha Jain",
    "vendor_avatar": "http://www.avatarsdb.com/avatars/dear_god.jpg",
    "vendor_type": "indivisual",
    "vendor_rating": "0",
    "vendor_phone": "9875463210",
    "vendor_address": "Golf Course Road Sector 54 New Vatika Towers Gurgaon",
    "vendor_neighbourhood": "Sector 54",
    "vendor_city": "gurgaon",
    "dish_id": "2",
    "dish_name": "Dal Baati Churma Thali",
    "dish_image": "http://s3-ap-southeast-1.amazonaws.com/elasticbeanstalk-ap-southeast-1-779583297639/Dish%2FImages%2FDal+Baati+Churma+Thali%2FNormal%2Fdal_baati_churma_thali-hdpi.jpg",
    "dish_description": "{\"main\":\"Dine in on a grand feast of authentic cuisine from Rajasthan. Panchmel Dal, Baati, Churma, Tadka Kadhi, Lehsun Chutney, Jeera Rice all come together to create a culinary memory that you would have seldom experience before.\",\"other\":\"Served as Panchmel Dal, 2 Baatis, Churma, Tadka Kadhi, Lehsun Chutney and Jeera Rice\"}",
    "dish_palate": "[\"2\",\"4\"]",
    "dish_tags": "Baati,Daal",
    "dish_cost": "200",
    "dish_price": "250",
    "dish_rating": "4.5",
    "$$hashKey": "object:7"
  }
]

【问题讨论】:

  • 你至少应该搜索一下吗?如果您是 android 开发新手,请学习 JSON Parsing
  • Stackoverflow 上已经存在很多问题/答案!
  • 是的,我进行了搜索,但我对访问菜肴描述中的主要标签感到困惑,因为括号在引号内。有人可以帮我吗?
  • 如果您不想手动解析并且您的 JSON 已修复,请使用 GSON 库!这样你只需要定义 POJO 类!

标签: android json multi-level


【解决方案1】:

请参考以下解析 json 响应的方法,如果您在字符串中获得响应,否则删除 JSONArray array = new JSONArray("res") 行,如果获取 json 数组。

try {
                    JSONArray array = new JSONArray("res");

                    for (int i = 0; i < array.length(); i++) {
                        JSONObject jsonObject = array.getJSONObject(i);

                        String vendor_name = jsonObject.getString("vendor_name");// same way parse all string


                        if(jsonObject.has("dish_description")){

                        String dish = jsonObject.getString("dish_description");

                        dish = dish.replaceAll("[\\W]", "");

                        JSONObject  jsonObject2 = new JSONObject(dish);

                        String  main = jsonObject2.getString("main");// same way get other one 


                        }


                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

【讨论】:

  • 我如何访问菜品描述的主要标签?因为括号在引号内
  • 你可以把它变成我提到的字符串,现在由你决定如何使用该字符串。
  • 除了字符串编辑还有其他方法吗?,因为我正在寻找嵌套的多级标签,但由于“dish_description”的嵌套括号包含在其字符串字段中,我无法使用它们!!
  • 您可以使用您的字符串进行编辑,但必须将其放入字符串中
  • 您的要求是什么?
猜你喜欢
  • 2012-05-10
  • 1970-01-01
  • 2018-10-23
  • 2018-06-18
  • 2022-11-02
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
相关资源
最近更新 更多