美文网首页
计算一个json的深度

计算一个json的深度

作者: 秋天de童话 | 来源:发表于2019-02-12 17:45 被阅读1次

计算一个json的深度

  <SCRIPT LANGUAGE="JavaScript">
     var json1 = "{'name':'t1','children':[{'children':[{'name':'t3'},{'name':'t4'},{'children':[{'name':'t5'},{'name':'t6'}]}]},{'name':'t2'}]}";
     var json2 ={
   "cluster": [{
      "id": "cluster1.1",
      "color": "blue",
      "flights": "784",
      "profit": "524125",
      "clv": "2364",
      "segment": [{
         "id": "segment1.1",
         "color": "green",
         "flights": "82",
         "profit": "22150",
         "clv": "1564",
         "node": [{
            "id": "node1.1",
            "color": "orange",
            "xpos": "1",
            "ypos": "1"
         }, {
            "id": "node1.2",
            "color": "orange",
            "xpos": "1",
            "ypos": "2"
         }, {
            "id": "node1.3",
            "color": "orange",
            "xpos": "1",
            "ypos": "3"
         }, {
            "id": "node1.4",
            "color": "orange",
            "xpos": "1",
            "ypos": "4"
         }]
      }, {
         "id": "segment1.2",
         "color": "red",
         "flights": "2",
         "profit": "2150",
         "clv": "1564",
         "node": [{
            "id": "node2.1",
            "color": "tan",
            "xpos": "2",
            "ypos": "1"
         }, {
            "id": "node2.2",
            "color": "tan",
            "xpos": "2",
            "ypos": "2"
         }, {
            "id": "node2.3",
            "color": "tan",
            "xpos": "2",
            "ypos": "3"
         }, {
            "id": "node2.4",
            "color": "tan",
            "xpos": "2",
            "ypos": "4"
         }]
      }]
   }, {
      "id": "cluster1.2",
      "flights": "4",
      "profit": "5245",
      "clv": "2364",
      "segment": [{
         "id": "segment1.2",
         "flights": "2",
         "profit": "2150",
         "clv": "1564",
         "node": [{
            "id": "node3.1",
            "xpos": "3",
            "ypos": "1"
         }, {
            "id": "node3.2",
            "xpos": "3",
            "ypos": "2"
         }, {
            "id": "node3.3",
            "xpos": "3",
            "ypos": "3"
         }, {
            "id": "node3.4",
            "xpos": "3",
            "ypos": "4"
         }]
      }]
   }, {
      "id": "cluster1.3",
      "flights": "10",
      "profit": "456978",
      "clv": "548",
      "segment": [{
         "id": "segment1.3",
         "flights": "2",
         "profit": "2150",
         "clv": "1564",
         "node": [{
            "id": "node4.1",
            "xpos": "4",
            "ypos": "1"
         }, {
            "id": "node4.2",
            "xpos": "4",
            "ypos": "2"
         }, {
            "id": "node4.3",
            "xpos": "4",
            "ypos": "3"
         }, {
            "id": "node4.4",
            "xpos": "4",
            "ypos": "4"
         }]
      }]
   }]
};

jsonDepper(json2);


function jsonDepper(json){
    var array = {'{':1,'}':-1},max=0,count=0;
    json = JSON.stringify(json);
     for (var i = 0, length = json.length; i < length; i++) {
         var result = array[json.charAt(i)];
         if (!result) continue;
             count+=result;
         if (count>max) {
             max = count;
         }
     }
     if (count != 0) {
         alert('json format error!');
     } else {
         alert('max:'+max);
     }
}

  </SCRIPT>

相关文章

  • 计算一个json的深度

    计算一个json的深度

  • 面试 | 卡掉不少人的一道腾讯算法面试题,高手来试试?

    算法题目 给定一个不确定的 Json 对象,求 Json 子节点的最大深度(编程语言不限,不可写伪代码)。如下: ...

  • 2-3变量类型和计算-3 如何理解json

    2-3变量类型和计算-3 如何理解json 问题:如何理解json json只不过是一个js对象而已 常用的api...

  • 2019-05-03

    深拷贝 利用JSON.stringify和JSON.parse实现深度拷贝 function copy(obj) ...

  • 吴恩达深度学习笔记(71)-带你了解计算机视觉(Computer

    计算机视觉(Computer vision) 计算机视觉是一个飞速发展的一个领域,这多亏了深度学习。 深度学习与计...

  • JSON.stringify的一些知识

    作用:我们一般会用json来将对象转为字符串,或者使用json.stringify和json.parse来实现深度...

  • golang小记

    深度拷贝map、struct用json.unmarshal,变成字符串,然后再用 json.marshal生成新的...

  • 树结构

    树的内部节点:非叶子节点树的外部节点:叶子节点 如何计算一个树的深度和高度 计算树的深度 假设p是树t中的一个节点...

  • CPU TFLOPS 计算

    深度学习任务是一个计算密集型任务,所以很关注计算设备的算力指标,因为目前深度学习还是以float32为主流,所以落...

  • 二叉树

    计算二叉树深度先计算左右子树的深度,然后整棵树的深度就是左右子树深度较大值加1(当前节点) 镜像二叉树 从上往下打...

网友评论

      本文标题:计算一个json的深度

      本文链接:https://www.haomeiwen.com/subject/izikeqtx.html