最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How frame Json object dynamically with dynamic key and value strings - Stack Overflow

programmeradmin6浏览0评论

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

var json = {};
var a = '"key1" : "value1"'; //ing as dynamic
var b = '"key2" : "value2"'; // ing 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.

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

var json = {};
var a = '"key1" : "value1"'; //ing as dynamic
var b = '"key2" : "value2"'; // ing 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.

Share Improve this question asked Sep 29, 2016 at 6:51 Sandeep sandySandeep sandy 3871 gold badge7 silver badges14 bronze badges 5
  • 1 try this array["key1"] = "value" – Hadi Commented Sep 29, 2016 at 6:53
  • Do you get var a = '"key1" : "value1"'; dynamically as a string ? – Ravi Teja Commented Sep 29, 2016 at 6:54
  • @SSH There's no associative arrays in JS – Rory McCrossan Commented Sep 29, 2016 at 7:01
  • "Unfortunately rather than taking dynamic value it is taking the reference variable itself " What do you mean by this? – Ravi Teja Commented Sep 29, 2016 at 7:15
  • Thankyou @SSH, Ravi, Rory. – Sandeep sandy Commented Sep 29, 2016 at 7:22
Add a ment  | 

1 Answer 1

Reset to default 7

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.

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:

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:

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.

发布评论

评论列表(0)

  1. 暂无评论