js中json的创建和调用

来源:互联网 发布:数字小姐是谁 知乎 编辑:程序博客网 时间:2024/05/22 03:35
var jn={
"search":{
"textValue":"1",
"object":{
"textValue":"1"
},
"type":{
"textValue":"1"
},
"id":{
"textValue":"1"
},
"query":{
"fatherid":"",
"relation":"",
"textValue":"1",
"id":{
"textValue":"1"
},
"name":{
"textValue":"1"
},
"filter":{
"relation":"",
"textValue":"1",
"operation":{
"textValue":"1"
},
"input":{
"textValue":"1",
}
},
"columns":{
"textValue":"1",
"column":{
"textValue":"1"
}
},
"storage":{
"textValue":"1",
"storetype":{
"textValue":"1"
},
"source":{
"textValue":"1"
}
}
},
"result":{
"textValue":"1",
"sort":{
"sortway":"",
"textValue":"1",
"sortitem":{
"textValue":"1",
"weight":""
}
},
"group":{
"textValue":"1",
"column":{
"textValue":"1"
}
},
"data":{
"textValue":"1",
"column":{
"textValue":"1"
}
},
"limit":{
"textValue":"1",
"offset":{
"textValue":"1"
},
"rows":{
"textValue":"1"
}
}
}
}

};

//     在这里时jn变量不是json对象,它只是一个普通的obj对象。

var str=JSON.stringify(jn); 

//          在这里我们把jn的obj转成了json的字符串形式,

console.info(str);

json=JSON.parse(str);

//          在这里我们把转好的json字符串变成json对象

console.info(json);


//       这个是能转成json的字符串形式。

                      var s="{\"dd\":{\"ss\":\"ww\",\"ee\":\"rr\"}}";
//将字符串变成json
json=JSON.parseJSON(json); 


//       这个是能转成json的字符串形式。

                      var s='{"dd":{"ss":"ww","ee":"rr"}}';
//将字符串变成json
json=JSON.parseJSON(json);       报错,不能转为json,因为它不认识'。只认识" 


0 0
原创粉丝点击