框架Json如何动态地使用动态键和值字符串

时间:2021-02-11 23:53:57

i have a json i want to add key value pairs (after framing the below format) like

我有一个json我想添加键值对(框架下面的格式后)像

var json = {};
var a = '"key1" : "value1"'; //coming as dynamic
var b = '"key2" : "value2"'; // coming as dynamic
json.push(a); // i know it is wrong.
json.push(b); // i know it is wrong.
console.log(json); // {"key1": "value1", "key2": "value2"} expected

var array = [];
var c = '{"key1": "value1"}';
var d = '{"key2": "value2"}';
array.push(c);
array.push(d);
console.log(array); // ["{"key1": "value1"}", "{"key2": "value2"}"] 

like the above i can push objects to array, but how can i push json strings directly to a json object.

像上面我可以将对象推送到数组,但我如何将json字符串直接推送到json对象。

1 个解决方案

#1


4  

Firstly a little clarification; there's no such thing as a 'JSON object'. There is a JSON-formatted string, and there is an object. They are two separate entities.

首先澄清一点;没有“JSON对象”这样的东西。有一个JSON格式的字符串,并且有一个对象。它们是两个独立的实体。

To add strings to an object, specify the property of the object and set its value. You don't need push() for this as that is used exclusively for arrays. In your case, it should look like this:

要向对象添加字符串,请指定对象的属性并设置其值。你不需要push(),因为它专门用于数组。在你的情况下,它应该是这样的:

var obj = {};
obj.key1 = "value1";
obj.key2 = "value2";
console.log(obj); // = { "key1": "value1", "key2": "value2" }

To set a key dynamically use bracket notation:

要动态设置密钥,请使用括号表示法:

var key = 'foo';
obj[key] = 'bar';
console.log(obj); // = { 'foo': 'bar' }

If you need to then convert the object to a JSON string, call JSON.stringify on that object:

如果您需要将对象转换为JSON字符串,请在该对象上调用JSON.stringify:

var json = JSON.stringify(obj);

Also note that in your second example you end up with an array of strings, not an array of objects. If you want an array of objects you need to remove the quotes around the values you set, like this:

另请注意,在第二个示例中,您最终会得到一个字符串数组,而不是一个对象数组。如果你想要一个对象数组,你需要删除你设置的值周围的引号,如下所示:

var array = [];
var c = { "key1": "value1" };
var d = { "key2": "value2" };
array.push(c);
array.push(d);
console.log(array); // [{ "key1": "value1" }, { "key2": "value2" }] 

Note the difference in the position of the quotes in the objects and the result from the console.

请注意对象中引号位置的差异以及控制台的结果。

#1


4  

Firstly a little clarification; there's no such thing as a 'JSON object'. There is a JSON-formatted string, and there is an object. They are two separate entities.

首先澄清一点;没有“JSON对象”这样的东西。有一个JSON格式的字符串,并且有一个对象。它们是两个独立的实体。

To add strings to an object, specify the property of the object and set its value. You don't need push() for this as that is used exclusively for arrays. In your case, it should look like this:

要向对象添加字符串,请指定对象的属性并设置其值。你不需要push(),因为它专门用于数组。在你的情况下,它应该是这样的:

var obj = {};
obj.key1 = "value1";
obj.key2 = "value2";
console.log(obj); // = { "key1": "value1", "key2": "value2" }

To set a key dynamically use bracket notation:

要动态设置密钥,请使用括号表示法:

var key = 'foo';
obj[key] = 'bar';
console.log(obj); // = { 'foo': 'bar' }

If you need to then convert the object to a JSON string, call JSON.stringify on that object:

如果您需要将对象转换为JSON字符串,请在该对象上调用JSON.stringify:

var json = JSON.stringify(obj);

Also note that in your second example you end up with an array of strings, not an array of objects. If you want an array of objects you need to remove the quotes around the values you set, like this:

另请注意,在第二个示例中,您最终会得到一个字符串数组,而不是一个对象数组。如果你想要一个对象数组,你需要删除你设置的值周围的引号,如下所示:

var array = [];
var c = { "key1": "value1" };
var d = { "key2": "value2" };
array.push(c);
array.push(d);
console.log(array); // [{ "key1": "value1" }, { "key2": "value2" }] 

Note the difference in the position of the quotes in the objects and the result from the console.

请注意对象中引号位置的差异以及控制台的结果。