• 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>

     一个应用示例:

    function btn_relateAdd() {
            if (!$('#relate').Validform()) {
                return false;
            }
            var relateData = [];
            var relateValue = $("#relateValue").val();
            if (relateValue != "") {
                var jsonObj = JSON.parse(relateValue);
                for (var i = 0; i < jsonObj.length; i++) {
                    relateData.push(jsonObj[i]);
                }
            } 
    
            var getData = $("#relate").GetWebControls();
            var postData = {};
            var isDuplication = false;
            postData["CompanyId"] = getData["RelateCompany"];
            postData["CompanyName"] = $("#RelateCompany").attr('data-text');
            postData["FlowName"] = $("#RelateFlow").attr('data-text');
            postData["FlowId"] = getData["RelateFlow"];
            postData["Amount"] = getData["Amount"];
            postData["Description"] = getData["Description"];
            //判断是否提交了重复数据
            for (var i = 0; i < relateData.length; i++) {
                if (postData["CompanyId"] == relateData[i]["CompanyId"] && postData["FlowId"] == relateData[i]["FlowId"]) {
                    isDuplication = true;
                    break;
                }
            }
            if (!isDuplication)
                relateData.push(postData);
            else
                dialogMsg("请不要重复添加", -1);
            $("#relateValue").val(JSON.stringify(relateData));
        }
    <input type="hidden" id="relateValue" />
  • 相关阅读:
    性能参考指标
    Java Native Interface 二 JNI中对Java基本类型和引用类型的处理
    Java Native Interface 编程系列一
    HTTP的报文与状态码
    [译]Android调整图像大小的一些方法
    Android多线程通信机制
    Android四大组件知识整理
    Java多态与反射
    23种设计模式的优点与缺点概况
    Android应用性能优化
  • 原文地址:https://www.cnblogs.com/firstcsharp/p/9154985.html
Copyright © 2020-2023  润新知