json - 使用路径检索json中的子级

标签 json dart

我有以下包含站点 map 数据的json。我也在导航中使用了这个json。

  {
        "home" : "home",
        "about" : "about",
        "contact" : "contact",
        "products" :
        {
            "home" : "home",
            "list" : "list",
            "product" :
            {
                "home" : "home",
                "specs" : "specs",
                "warranty" : "warranty",
                "related" : "related"
            }
        } }

程序启动后,此json将存储在这样的变量中
Future _loadData()
{
    return _http.get(url)
    .then((response){
        nav = response.data;
    });
}

此外,我有一个列表/数组来获取当前页面和路径

List ---- home>products>product



现在用户在产品页面中,我需要将产品的子级显示为导航链接
  • 主页
  • 规格
  • 保修
  • 相关的

  • 三个相关问题:
    我该怎么做呢?
    使用json存储和检索站点 map 数据是一个好主意吗?
    xml套件会这样吗?

    最佳答案

    首先,您应该将其转换为Dart数据结构

    import 'dart:convert' show JSON;
    
    Map sitemap = JSON.decode(nav);
    

    然后您可以在调试器中研究sitemap变量,以查看其结构并在其中访问数据

    print(sitemap['products']['product']['warranty']);
    

    (应打印warranty

    您还可以遍历数据

    sitemap.keys.forEach((e) {
      print(e);
    });
    

    关于json - 使用路径检索json中的子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25090844/

    相关文章:

    javascript - Selenium - 将文件写入带有换行符的 codemirror

    json - Flutter如何从JSON HTTP响应中获取特定值

    http - 如何在正文中发送嵌套的 Json 作为来自 Flutter 的 http post 请求

    mysql - Erlang json验证问题

    javascript - 将对象数组的 AJAX 响应转换为 JSON 对象

    generics - 将类型/类作为参数传递并访问静态属性、构造函数等

    flutter - flutter 中 firestore 中的对象是否有转换器方法?

    flutter - Firestore错误 'controller != null':不正确

    php - 显示本地存储的图像以响应 JSON 表填充结果

    Ruby json解析错误: unexpected token