Given this object
Object { value=584, end_time="2013-11-03T07:00:00+0000"}
How do I convert to proper JSON?
JSON.stringify
doesn't wrap double-quotes around keys/values without them in the object. I just get:
{"value":584,"end_time":"2013-11-03T07:00:00+0000"}
When you need value
to be of type "string" you must iterate over the items and convert the type of each single item.
simple example:
objects=[{ value:584},{ value:123},{ value:456}];
console.log('before:',objects);
//before: [Object { value=584}, Object { value=123}, Object { value=456}]
$.each(objects,function(i,v){objects[i].value = String(v.value)});
console.log('after:',objects);
//after: [Object { value="584"}, Object { value="123"}, Object { value="456"}]
console.log('JSON-string:',JSON.stringify(objects));
//JSON-string: [{"value":"584"},{"value":"123"},{"value":"456"}]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.