引用JSON树结构的分支的最佳方法是什么? [英] What are the best ways to reference branches of a JSON tree structure?

查看:193
本文介绍了引用JSON树结构的分支的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个JSON文件被解析为Javascript中的对象。我知道你在想什么:幸运的家伙。 JSON本质上是一种大树形式的流程图。以下是我想要实现的一个小样本:

So I've got a JSON file that gets parsed into an object in Javascript. I know what you're thinking: lucky guy. The JSON is essentially a flow diagram in a big tree form. Here's tiny a sample of what I'm trying to achieve:

tree = {
    "options": [
        {
            "options": [
                {
                    "name": "target",
                },
            ],
        },
        {
            "options": [
                {
                    "link": "...?",
                },
            ],
        },
    ]
}

所以在这个例子中,我会深入第二个分支(它说link),我希望能够跳转到包含name的分支:目标。这是JSON记得所以它需要是一个字符串(除非有原生的链接?!有吗?)但我不知道如何最好的格式化。

So in this example, I'll be deep in the second branch (where it says "link") and I'll want to be able to jump to the branch that contains "name": "target". This is JSON remember so it'll need to be a string (unless there's a native for linking?! is there?) but I don't know how best to format that.

在我看来,我至少有几个选择。

As I see it, I've got at least a couple of options.


  1. 我可以搜索。如果 name 是唯一的,我可以缩放树寻找元素,直到找到它为止。我之前从未使用过Javascript,但我认为它很慢。

  1. I could search. If name was unique, I could scale the tree looking for elements until I found it. I've never done with with Javascript before but I expect it to be slow.

我可以使用像这样的导航路径选项: 1:options:1 ,描述路径的每个键。再说一次,我从来没有这样做过,但假设没有错误,那就快了很多。你会如何实现它?

I could use a navigation path like options:1:options:1 that describes each key for the path. Again, I've never done this but, assuming there are no errors, it would be a lot faster. How would you implement it?

我还有其他选择吗?最好的是什么?有没有办法在JSON解码时解压缩,或者是无限循环的配方?

Are there any other options available to me? What seems best? Is there a way to unpack this when JSON decoding, or is that a recipe for an infinite loop?

推荐答案

怎么样 link:'tree.options [0] .options [0]'然后 eval(path.to.link)

以下样本仅使用Chrome进行测试。同一棵树:

Following samples were tested with Chrome only. Same tree for all :

var tree = { level1: [{ key: 'value' }] };






eval




No eval

function resolve(root, link) {
    return (new Function('root', 'return root.' + link + ';'))(root);
}

var value = resolve(tree, path.to.link);






回退到窗口




Fallback to window

function resolve(root, link) {
    return (new Function(
        'root', 'return root.' + (link || root) + ';'
    ))(link ? root : window);
}

resolve(tree, 'level1[0].key'); // "value"
resolve('tree.level1[0].key'); // "value"






捕获错误



try / catch块可防止链接中断错误。


Catching errors

The try/catch block prevents broken links from throwing errors.

function resolve(root, path) {
    try {
        return (new Function('root', 'return root.' + path + ';'))(root);
    } catch (e) {}
}

resolve(tree, 'level1[0].key'); // "value"
resolve(tree, 'level1[1].key'); // undefined






使用自定义路径格式



这里的好处是我们可以将对象或数组作为 root 传递。另请注意,我们可以使用我们选择的任何字符替换 path.split('/')中的斜杠。


Using custom path format

The good part here is that we can pass either an object or an array as root. Also note that we can replace the slash in path.split('/') with any char of our choice.

function resolve(root, path) {
    path = '["' + path.split('/').join('"]["') + '"]';
    return (new Function('root', 'return root' + path + ';'))(root);
}

resolve(tree.level1, '0/key'); // "value"
resolve(tree, 'level1/0/key'); // "value"
resolve(tree, 'level1/0'); // Object {key: "value"}

这篇关于引用JSON树结构的分支的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆