jQuery 附加 JSON 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10489332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:25:12  来源:igfitidea点击:

Append JSON Strings

jqueryjsonstringappendconcat

提问by mbark

I'm having a little trouble with some JS and hope someone can help.

我在使用某些 JS 时遇到了一些麻烦,希望有人可以提供帮助。

I have two JSON strings that are collected via AJAX using jQuery and then parsed accordingly. The issue I have is that if I ever want to add new features to the application I am making, the JSON strings are statically stored in the database and don't change if new attributes are added to the default JSON string.

我有两个 JSON 字符串,它们使用 jQuery 通过 AJAX 收集,然后进行相应的解析。我遇到的问题是,如果我想向我正在制作的应用程序添加新功能,JSON 字符串静态存储在数据库中,如果新属性添加到默认 JSON 字符串中,则不会更改。

var string1 = {
    "col1": [
        {
            "foo": "bar"
        }
    ],
    "col2": [
        {
            "foo": "bar",
            "bar": "foo"
        }
    ],
    "col3": [
        {
            "foo": "bar"
        }
    ]
}

var string2 = {
    "col1": [
        {
            "foo": "bar"
        }
    ],
    "col2": [
        {
            "foo": "bar"
        }
    ]
}

string2is the user saved the script, hypothetically saved three days ago. Since then, the default string string1has been added to, and the information needs to be added to string2.

string2是用户保存的脚本,假设是三天前保存的。此后,string1添加了默认字符串,需要将信息添加到string2.

Can anyone help with this?

有人能帮忙吗?

回答by Sampo Sarrala - codidact.org

This will do:

这将:

// Input:
var json_string = '{"name":"John"}';

// Convert JSON array to JavaScript object
var json_obj = JSON.parse( json_string );

// Add new key value pair "myData": "Helo World" to object
json_obj.myData = 'Hello World';

// Another more dynamic way to add new key/value pair
json_obj['myAnotherData'] = 'FooBar';

// Convert back to JSON string
json_string = JSON.stringify( json_obj );

// Log to console:
console.log( json_string );

Output:

输出:

{
    "name": "John",
    "myData": "Hello World",
    "myAnotherData": "FooBar"
}

jQuery JSON parser:

jQuery JSON 解析器:

If you want to use jQuery parser or need support for IE7 you can replace

如果你想使用 jQuery 解析器或者需要支持 IE7 你可以替换

var json_obj = JSON.parse( json_string );

with

var json_obj = $.parseJSON( json_string );

Unfortunately, jQuery cannot convert json_objectback to JSON string, you will need another plugin or library for that.

不幸的是,jQuery 无法转换json_object回 JSON 字符串,为此您需要另一个插件或库。

More on topic:

更多主题:

MDN documentation

MDN 文档

Browser support

浏览器支持

回答by Shakti Srivastav

use back slash:

使用反斜杠:

"{\"id\": \"abc123\", \"success\": true}"

"{\"id\": \"abc123\", \"success\": true}"

回答by Elliot Bonneville

Untested, but this should work:

未经测试,但这应该有效:

function updateJsonString(oldString, newString) {
    var oldObj = JSON.parse(oldString), newObj = newString.parse(newString);
    for(prop in newObj) {
        if(newObj.hasOwnProp(prop)) {
            if(!oldObj.hasOwnProp(prop)) {
                oldObj[prop] = newObj[prop];
            }
        }
    }

    return JSON.stringify(oldObj);
}

var updatedString = updateJsonString(string2, string1);