迭代嵌套的JSON树并更改值 [英] Iterate through nested JSON tree and change values

查看:96
本文介绍了迭代嵌套的JSON树并更改值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的是具有以下结构的JSON树:

What I have is a JSON tree of the following structure:

{
    "projects": {
        "Proj1": {
            "milestones": {
                "default": "20150101",
                "default2": "20140406",
                "default3": "20140101",
                "default4": "20131231",
                "default5": "20131220"
            }
        },
        "Proj2": {
            "milestones": {
                "default": "20131231",
                "default2": "20131220"
            }
        }
    }
}

我有代码将其读入网页,文本中包含默认部分,以及数字/日期在文本框/表单中。这个想法是你可以改变日期并提交,然后提交到后端并写入文件。所有这些在大多数情况下都有效。我能弄清楚的是如何遍历我拥有的JSON树并编写新值。例如:

I have code to read it into a web page, with the 'default' part in text, and the numbers/dates in a text box/form. The idea is that you can change the dates and submit, which goes to the backend and gets written to a file. All that works for the most part. What I can figure out is how to iterate through the JSON tree I have and write the new values. For example:

访问JSONTREE.projects.Proj1.milestones.default会返回该键的值。使用该调用设置值会适当地更改该值。我想要做的是迭代整个树,并根据表单框中的内容设置'默认值'的值。我有这个:

Accessing JSONTREE.projects.Proj1.milestones.default returns the value for that key. Setting the value with that call changes the value appropriately. What I want to do is iterate through the entire tree and set the values of the 'defaults' based on whatever is in the form box. I have this:

$.each(formJSON.projects, function (projectName) {
        $.each(this, function (selection) {
            $.each(this, function (milestones, date) {
                var saltKey = projectName + "-" + milestones;
                date = document.getElementById(saltKey).value;
           });
       });
});

但它什么都不做,即使'alert(date)'返回一个值。我怀疑这是因为它是值,而不是对象的引用,但我怎样才能到达对象?我怀疑它很简单,但我不是jQuery / JS的专家。

but it does nothing, even though 'alert(date)' returns a value. I suspect this is because it's the value, and not a reference to the object, but how can I get to the object? I suspect it's simple, but I'm not a pro with jQuery/JS.

TL; DR如何在嵌套的JSON树中获取对密钥的引用,所以我可以改变价值吗?

TL;DR How do I get references to a key in a nested JSON tree so I can change the value?

编辑:好的,我认为这就是我需要的: JS / Jquery - 在json选择器中使用变量。我将'date'部分更改为:formJSON.projects [projectName] [selection] [里程碑] = document.getElementById(saltKey).value;这似乎有效。

Okay, I think this is what I needed: JS/Jquery - using variable in json selector. I changed the 'date' portion to: formJSON.projects[projectName][selection][milestones] = document.getElementById(saltKey).value; which seems to work.

推荐答案

您编辑传递的值而不是原始的JSON对象。

Your editing the passed value and not the original JSON object.

解决此问题的一种方法是创建一个新的JSON对象,在遍历现有JSON对象时进行构建,然后覆盖原始对象或使用新的JSON对象。

One way to fix this is to create a new JSON object, build as you iterate through the existing one, and then overwrite the original or use the new JSON object.

另一种方法是创建一个包含原始JSON对象的var,并将其传递给函数或直接在函数内部访问它。

Another is to create a var holding the original JSON object and either pass it through your functions or access it directly inside the functions.

这篇关于迭代嵌套的JSON树并更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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