Convert JS object to JSON string

后端 未结 27 2495
庸人自扰
庸人自扰 2020-11-22 00:43

If I defined an object in JS with:

var j={\"name\":\"binchen\"};

How can I convert the object to JSON? The output string should be:

27条回答
  •  粉色の甜心
    2020-11-22 01:47

    One custom defined for this , until we do strange from stringify method

    var j={"name":"binchen","class":"awesome"};
    var dq='"';
    var json="{";
    var last=Object.keys(j).length;
    var count=0;
    for(x in j)
    {
    json += dq+x+dq+":"+dq+j[x]+dq;
    count++;
    if(count

    OUTPUT

    {"name":"binchen","class":"awesome"}
    

    LIVE http://jsfiddle.net/mailmerohit5/y78zum6v/

提交回复
热议问题