【问题标题】:How to read json array value and its child array using jquery如何使用 jquery 读取 json 数组值及其子数组
【发布时间】:2016-06-04 17:55:14
【问题描述】:

您好,我有一个以下格式的 json 数组。

{
    "data": {
        "title": "MainNode"
    },
    "children": [{
        "data": {
            "title": "Firstchild",
            "description": "Texttexttext"
        },
        "children": [{
            "data": {
                "title": "Firstchildschild",
                "description": "Texttexttext"
            },
            "children": [{
                "data": {
                    "title": "FirstchildschildsChild",
                    "description": "Texttexttext"
                }
            }]
        }, {
            "data": {
                "title": "FirstchildsSecondchild",
                "description": "Texttexttext"
            },
            "children": [{
                "data": {
                    "title": "FirstchildsSecondchildsChild",
                    "description": "Texttexttext"
                }
            }]
        }]
    }]
}

我必须根据标题文本值读取数组并列出该特定数据及其子数据。

例如。如果我的输入(标题值将从 http URL 获取,如下所示)是 "Firstchild/Firstchildschild/",那么我必须列出 FirstchildschildsChild 及其描述。

如果我的输入是 /Firstchild/,那么我必须列出 Firstchildschild & FirstchildsSecondchild 数据及其子名称。

使用 jquery 如何获取我上面提到的记录。请告知处理此 json 而不是使用大量循环的最佳方法?

【问题讨论】:

标签: javascript jquery arrays json tree


【解决方案1】:

好吧,只是为了这些必需品(并且相信这确实是一个很大的必需品),我发明了一个名为 Object.prototype.getNestedValue() 的 Object 方法,它可以动态地从深度嵌套的 Objects 中获取嵌套值。您需要提供的只是属性的字符串和数组索引的正确顺序的数字。好的,让我们看看...

Object.prototype.getNestedValue = function(...a) {
  return a.length > 1 ? (this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]];
};

var JSONobj = {
    "data": {
        "title": "MainNode"
    },
    "children": [{
        "data": {
            "title": "Firstchild",
            "description": "Texttexttext"
        },
        "children": [{
            "data": {
                "title": "Firstchildschild",
                "description": "Texttexttext"
            },
            "children": [{
                "data": {
                    "title": "FirstchildschildsChild",
                    "description": "Texttexttext"
                }
            }]
        }, {
            "data": {
                "title": "FirstchildsSecondchild",
                "description": "Texttexttext"
            },
            "children": [{
                "data": {
                    "title": "FirstchildsSecondchildsChild",
                    "description": "Texttexttext"
                }
            }]
        }]
    }]
},
     value = JSONobj.getNestedValue("children",0,"children",0,"data","title");
console.log(value); // Firstchildschild
var a = "children",
    b = 0,
    c = "data",
    d = "title";
    value2 = JSONobj.getNestedValue(...[a,b,a,b,c,d])
    console.log(value2); // Firstchildschild

它还有一个妹妹叫Object.prototype.setNestedValue()

【讨论】:

  • 这对这里有什么帮助? OP 应该如何将输入 (Firstchild/Firstchildschild) 映射到属性?
  • 如果你仔细阅读这个问题,你会明白“通过依赖”从 HTTP URL 接收到的信息,OP 需要访问一个嵌套属性及其值。这称为动态访问,您无法使用静态代码(没有无数 if 和 else)来完成这项工作。所以这个解决方案出现了,每个人都非常高兴,因为他们可以从他们的输入中整理出要传递给这个函数的参数。
  • “提供他们的输入,他们可以整理出传递给这个函数的参数” 我认为这是更大的问题。如果你想一想,如果他们能从输入中找出那些参数,他们就不再需要你的函数了,因为他们已经有了他们正在寻找的价值。此外,您的代码解决了has already been solved before 的问题,那么您为什么不关闭/将问题标记为重复(如果您认为这是问题的解决方案)?
  • 当然不是。我不是在这里用银勺喂人。 :) OP 仍然需要对他的输入进行一些处理,以找出他必须走多少级并将必要的参数插入到数组中(请参阅我的最后一个示例,其中包含 vars a、b、c、d)并将该数组提供给 @ 987654326@。他的数组结构非常好,应该没什么大不了的。
  • 您可以从输入中解析路由,但您绝对不能使用静态代码访问该值(您需要 eval)。我最近看到这类问题出现得太频繁了,尽管我看过我还没有看到任何动态访问解决方案(除了我不喜欢的一个),所以我做了我的。它有什么问题..?
【解决方案2】:
var obj = jQuery.parseJSON( '
    "data": {
        "title": "MainNode"
    },
    "children": [{
        "data": {
            "title": "Firstchild",
            "description": "Texttexttext"
        },
        "children": [{
            "data": {
                "title": "Firstchildschild",
                "description": "Texttexttext"
            },
            "children": [{
                "data": {
                    "title": "FirstchildschildsChild",
                    "description": "Texttexttext"
                }
            }]
        }, {
            "data": {
                "title": "FirstchildsSecondchild",
                "description": "Texttexttext"
            },
            "children": [{
                "data": {
                    "title": "FirstchildsSecondchildsChild",
                    "description": "Texttexttext"
                }
            }]
        }]
    }]
}' );

alert( obj.name === "data" );

你必须解析元素

obj.name === "数据"

在下link

【讨论】:

  • 问题不是关于解析 json ......而是关于过滤结果。更不用说你的alert() 没有意义
猜你喜欢
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 2016-04-15
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2016-12-08
相关资源
最近更新 更多