您的位置:首页 > Web前端 > JavaScript

js中json的创建和调用

2016-07-22 11:37 363 查看
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,因为它不认识'。只认识" 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: