zoukankan      html  css  js  c++  java
  • json字符串、对象、数组之间的转换方法

    json字符串转化成json对象

    1.jquery的方法: var jsonObj =$.parseJSON(jsonStr)

    2.js 的方法: var jsonObj =JSON.parse(jsonStr);    

    json对象转化成json字符串

    js方法var jsonStr1 =JSON.stringify(jsonObj)

    <script type="text/javascript">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部门"}]';//  var jsonObj = $.parseJSON(jsonStr);var jsonObj =JSON.parse(jsonStr)
            console.log(jsonObj)
         var jsonStr1 =JSON.stringify(jsonObj)
         console.log(jsonStr1+"jsonStr1")
        </script>

    json对象转化成数组

    <script type="text/javascript">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部门"}]';//  var jsonObj = $.parseJSON(jsonStr);var jsonObj =JSON.parse(jsonStr)
            console.log(jsonObj)
         var jsonStr1 =JSON.stringify(jsonObj)
         console.log(jsonStr1+"jsonStr1")
         var jsonArr = [];for(var i =0;i <jsonObj.length;i++){
                jsonArr[i] = jsonObj[i];}console.log(typeof(jsonArr))
        </script>
  • 相关阅读:
    CF763C Timofey and Remoduling
    CF762E Radio Stations
    CF762D Maximum Path
    CF763B Timofey and Rectangles
    URAL1696 Salary for Robots
    uva10884 Persephone
    LA4273 Post Offices
    SCU3037 Painting the Balls
    poj3375 Network Connection
    Golang zip压缩文件读写操作
  • 原文地址:https://www.cnblogs.com/linzhijie45/p/6751588.html
Copyright © 2011-2022 走看看