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

json - Stringify JavaScript object - Stack Overflow

programmeradmin4浏览0评论

I'm looking to stringify an object.

I want in output like this

{"1":{"valeur":"dalebrun","usager":"experttasp","date":"2013-08-20 16:41:50"}, "2": {"valeur":"test","usager":"experttasp","date":"2013-08-20 16:41:50"}}

But I get this

{"valeur":"dalebrun","usager":"experttasp","date":"2013-08-20 16:41:50"}, {"valeur":"test","usager":"experttasp","date":"2013-08-20 16:41:50"}

What I do

var objVal = {}; //value....
var data = {}; //other value....
var object = $.extend({}, objVal, data); //concat the object 
JSON.stringify(object); 

I'm looking to stringify an object.

I want in output like this

{"1":{"valeur":"dalebrun","usager":"experttasp","date":"2013-08-20 16:41:50"}, "2": {"valeur":"test","usager":"experttasp","date":"2013-08-20 16:41:50"}}

But I get this

{"valeur":"dalebrun","usager":"experttasp","date":"2013-08-20 16:41:50"}, {"valeur":"test","usager":"experttasp","date":"2013-08-20 16:41:50"}

What I do

var objVal = {}; //value....
var data = {}; //other value....
var object = $.extend({}, objVal, data); //concat the object 
JSON.stringify(object); 
Share Improve this question edited Aug 22, 2013 at 15:01 davidlebr1 asked Aug 22, 2013 at 14:51 davidlebr1davidlebr1 4332 gold badges6 silver badges16 bronze badges 4
  • you have var date and later you use data? – Ron van der Heijden Commented Aug 22, 2013 at 14:53
  • oups sorry it's data not date – davidlebr1 Commented Aug 22, 2013 at 14:58
  • I can think of no better use case for vote to close a question as being unclear, as when you provide an answer to the OP as I did, and they say it works but it's not what they're looking for – Dexygen Commented Aug 22, 2013 at 20:34
  • The answer of Giovanni was the closest answer. But thank you for your help and time @George Jempty. I did not say that you did not do a OP answer.. – davidlebr1 Commented Aug 22, 2013 at 21:58
Add a comment  | 

2 Answers 2

Reset to default 6

When you concat the object, you get an array; you want a map with two elements, using the id "1" and "2"

var objVal = {};   //value....
var data = {};     //other value....

var object = {}
object["1"] = objVal;
object["2"] = date;
JSON.stringify(object); 

I found the solution !

I do an for loop on the object. And I iterate on each element in the object. Thank you for your help. The answer of @Giovanni help me to found the solution.

Solution:

var data = {}; //values....
var objVal = {}; //other values....
var final = {};
var index = 1;
for(var key in data)
{
    final[index] = data[key];
    index = index + 1;
}
final[index] = objVal;
JSON.stringify(final);

And the output is :

{"1":{"valeur":"dfgdfg","usager":"experttasp","date":"2013-08-23 10:36:54"},"2":{"valeur":"uuuuuuuuuu","commentaire":"defg","usager":"experttasp","date":"2013-08-23 10:37:26"},"3":{"valeur":"uuuuuuuuuu","commentaire":"yesssss","usager":"experttasp","date":"2013-08-23 10:38:38"}}
发布评论

评论列表(0)

  1. 暂无评论