• json字符串、json对象、数组 三者之间的转换


    json字符串转化成json对象

    // jquery的方法
    var jsonObj = $.parseJSON(jsonStr)
    //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>
  • 相关阅读:
    HttpContext.GetOwinContext().Authentication 报错 解决办法
    owin Claims-based认证登录实现
    angularjs初识ng-app、ng-model、ng-repeat指令
    SpringBoot配置slf4j logback-spring.xml日志
    idea时间注释模版
    oracel截取字符串
    win10官网下载地址
    使用HttpWebRequest实现basic身份认证
    mybatis常用jdbcType数据类型与mysql的类型对照
    修改IntelliJ IDEA 默认配置路径
  • 原文地址:https://www.cnblogs.com/AiMuzi/p/6646640.html
Copyright © 2020-2023  润新知