json字符串json对象数组 三者之间的转换
Posted ZhiPeng007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json字符串json对象数组 三者之间的转换相关的知识,希望对你有一定的参考价值。
json字符串转化成json对象
1 // jquery的方法 2 var jsonObj = $.parseJSON(jsonStr) 3 //js 的方法 4 var jsonObj = JSON.parse(jsonStr)
json对象转化成json字符串
1 //js方法 2 var jsonStr1 = JSON.stringify(jsonObj)
例如
例如:
1 <script type="text/javascript"> 2 var jsonStr = ‘[{"id":"01","open":false,"pId":"0","name":"A部门"},{"id":"01","open":false,"pId":"0","name":"A部门"},{"id":"011","open":false,"pId":"01","name":"A部门"},{"id":"03","open":false,"pId":"0","name":"A部门"},{"id":"04","open":false,"pId":"0","name":"A部门"}, {"id":"05","open":false,"pId":"0","name":"A部门"}, {"id":"06","open":false,"pId":"0","name":"A部门"}]‘; 3 // var jsonObj = $.parseJSON(jsonStr); 4 var jsonObj = JSON.parse(jsonStr) 5 console.log(jsonObj) 6 var jsonStr1 = JSON.stringify(jsonObj) 7 console.log(jsonStr1+"jsonStr1") 8 </script>
json对象转化成数组
1 <script type="text/javascript"> 2 var jsonStr = ‘[{"id":"01","open":false,"pId":"0","name":"A部门"},{"id":"01","open":false,"pId":"0","name":"A部门"},{"id":"011","open":false,"pId":"01","name":"A部门"},{"id":"03","open":false,"pId":"0","name":"A部门"},{"id":"04","open":false,"pId":"0","name":"A部门"}, {"id":"05","open":false,"pId":"0","name":"A部门"}, {"id":"06","open":false,"pId":"0","name":"A部门"}]‘; 3 // var jsonObj = $.parseJSON(jsonStr); 4 var jsonObj = JSON.parse(jsonStr) 5 console.log(jsonObj) 6 var jsonStr1 = JSON.stringify(jsonObj) 7 console.log(jsonStr1+"jsonStr1") 8 var jsonArr = []; 9 for(var i =0 ;i < jsonObj.length;i++){ 10 jsonArr[i] = jsonObj[i]; 11 } 12 console.log(typeof(jsonArr)) 13 </script>
以上是关于json字符串json对象数组 三者之间的转换的主要内容,如果未能解决你的问题,请参考以下文章