【问题标题】:JSON data should not have key value in numeric and result not coming as per expected resultJSON 数据不应具有数字键值,并且结果不符合预期结果
【发布时间】:2017-11-03 00:39:40
【问题描述】:

我正在使用递归创建带有嵌套集模型的 JSON。我的结果没有像预期的那样出现,因为这个 JSON 帮助我生成了一棵树。括号不是必需的 JSON。

我正在尝试创建一个像 http://fperucic.github.io/treant-js/examples/evolution-tree/example6.js 这样的 json。我对 nodeStructure: { }

感兴趣

问题:

  1. 每个孩子都有 { } 但需要 [ ]

  2. 不需要数字键

  3. json 键不应出现在 "text"、"children"、"name" 等引号中,而应不含引号

在线编译器:https://3v4l.org/UsXPv

<?php
  $category = '{"9":{"id":"9","btc_mlm_user_id":"0","lft":"1","rht":"16","lvl":"0","name":"Root","created":"2017-06-27 05:56:11","modified":"2017-06-27 05:56:11","first_name":"","last_name":"","username":""},"42":{"id":"42","btc_mlm_user_id":"25","lft":"2","rht":"13","lvl":"1","name":"naresh","created":"2017-11-02 10:22:24","modified":"2017-11-02 10:22:24","first_name":"","last_name":"","username":"naresh"},"44":{"id":"44","btc_mlm_user_id":"27","lft":"3","rht":"4","lvl":"2","name":"rahul1","created":"2017-11-02 10:25:53","modified":"2017-11-02 10:25:53","first_name":"","last_name":"","username":"rahul1"},"45":{"id":"45","btc_mlm_user_id":"28","lft":"5","rht":"6","lvl":"2","name":"rahul123","created":"2017-11-02 10:27:19","modified":"2017-11-02 10:27:19","first_name":"","last_name":"","username":"rahul123"},"46":{"id":"46","btc_mlm_user_id":"29","lft":"7","rht":"12","lvl":"2","name":"kapil1","created":"2017-11-02 10:28:20","modified":"2017-11-02 10:28:20","first_name":"","last_name":"","username":"kapil1"},"47":{"id":"47","btc_mlm_user_id":"30","lft":"8","rht":"11","lvl":"3","name":"priya12","created":"2017-11-02 10:30:30","modified":"2017-11-02 10:30:30","first_name":"","last_name":"","username":"priya12"},"48":{"id":"48","btc_mlm_user_id":"31","lft":"9","rht":"10","lvl":"4","name":"amit12","created":"2017-11-02 10:32:00","modified":"2017-11-02 10:32:00","first_name":"","last_name":"","username":"amit12"},"43":{"id":"43","btc_mlm_user_id":"26","lft":"14","rht":"15","lvl":"1","name":"roshan","created":"2017-11-02 10:24:27","modified":"2017-11-02 10:24:27","first_name":"","last_name":"","username":"roshan"}}';

  function tree($data, $left = 0, $right = null) 
  {
    $tree = array();
    foreach ($data as $key => $value) 
    {
      if ($value['lft'] == $left + 1 && (is_null($right) || $value['rht'] < $right)) 
      {
        $tree[$key]['text']  = ['name' => $value['name']];
        $tree[$key]['children'] = tree($data, $value['lft'], $value['rht']);
        $left = $value['rht'];
      }
    }
    return $tree;
  }

  $tree = tree(json_decode($category, true));
  echo json_encode($tree);

输出:

{
  "9": {
    "text": {
      "name": "Root"
    },
    "children": {
      "42": {
        "text": {
          "name": "naresh"
        },
        "children": {
          "44": {
            "text": {
              "name": "rahul1"
            },
            "children": []
          },
          "45": {
            "text": {
              "name": "rahul123"
            },
            "children": []
          },
          "46": {
            "text": {
              "name": "kapil1"
            },
            "children": {
              "47": {
                "text": {
                  "name": "priya12"
                },
                "children": {
                  "48": {
                    "text": {
                      "name": "amit12"
                    },
                    "children": []
                  }
                }
              }
            }
          }
        }
      },
      "43": {
        "text": {
          "name": "roshan"
        },
        "children": []
      }
    }
  }
}

需要的输出:

{
  text: {
    name: "Root"
  },
  children: [{
    text: {
      name: "naresh"
    },
    children: [{
      text: {
        name: "rahul1"
      },
      children: [
        []
      ],
      text: {
        name: "rahul123"
      },
      children: [
        []
      ],
      text: {
        name: "kapil1"
      },
      children: [{
        text: {
          name: "priya12"
        },
        children: [{
          text: {
            name: "amit12"
          },
          children: [
            []
          ]
        }]
      }]
    }],
    text: {
      name: "roshan"
    },
    children: [
      []
    ]
  }]
}

这是我在此处获取的 MySql 记录,以便在开始时以 $category json 格式向您展示。

【问题讨论】:

  • 您的实际输出和所需输出似乎相同。你是不是贴错东西了?
  • @ADyson 抱歉 - 这是我在编辑时的错误。不是 OP 的错。
  • @Johan 您尝试创建的数据结构无效,因为您在对象中重复了相同的键名
  • 您的“必需”输出仍然无效,因此我们无法帮助您实现这一目标。例如,您不能在同一个对象中拥有多个名为“文本”的属性。 children 需要是多个对象的数组,而不是您现在拥有的包含单个对象(然后包含多个重复属性)的数组。
  • 好吧,这与您在问题中发布的所需结构不同,是吗?在这种情况下,请用您实际想要的内容更新问题。如果您看不出区别,请仔细查看您链接到的示例。

标签: php json


【解决方案1】:

如果您想获得output you linked,而不是您问题中的那个(as pointed by @RoryMcCrossan 无效,因为它包含每个对象的多个相等键),那么您可以将代码更改为:

function tree($data, $left = 0, $right = null) {
    $tree = array();
    foreach ($data as $key => $value) {
        if ($value['lft'] == $left + 1 && (is_null($right) || $value['rht'] < $right)) {
            $child = []; // Let's make a new child
            $child['text'] = ['name' => $value['name']]; // The text is required
            $childTree = tree($data, $value['lft'], $value['rht']); // Let's find its children
            if (!empty($childTree)) { // If it has children
                $child['children'] = $childTree; // Let's save the children
            }
            $tree[] = $child; // Put the child in the tree
            $left = $value['rht'];
        }
    }
    return $tree;
}

$tree = tree(json_decode($category, true))[0]; // Since there's only one root, you want the first element of the tree

这是完整代码:https://3v4l.org/AYCGt

这只会给你留下一个问题,根据你的说法,键不应该有引号。虽然我真的不知道你的动机,它应该与 Javascript 中的引号一起使用,但你可以使用 preg_replace 进行一些替换,如下所示:

echo preg_replace('/"(\w+)":/','$1:',json_encode($tree));

这将是完整的代码:https://3v4l.org/ZaXip

【讨论】:

  • “键不应该有引号”。真的吗?为什么?最严格的 JSON 标准实际上要求他们这样做。浏览器通常更宽松一点,但它们并非必须如此。尝试通过 jsonlint.comjsonformatter.curiousconcept.com 解析某些内容,但不带引号...
  • 嗯,这就是@Johan 所说的。我反对它,但也许使用代码是为了生成尽可能干净的 Javascript。也许这就是这个问题曾经有 Javasript 标签的原因。也许 JSON 会浪费字符
  • 不幸的是,基于上面的其他 cmets,OP 似乎不理解(并且似乎不想理解)有效 JSON 的构成,所以也许这与它有关,但我不能不能肯定地说。 Javascript 并不真正关心是否使用引号,所以很难看出它为什么重要——通常没有人会真正看到这样的数据。最好生成符合标准的东西,而 json_encode() 会自动执行此操作。只是我的意见,但删除引号似乎是浪费时间和 CPU。
  • 我同意你的观点,这似乎是在浪费资源,但话说回来,我们真的不知道@Johan 想要的最终代码是什么。如果 JSON 不是真的需要,那么使用引号是无关紧要的,因为它们会浪费空间。所以也许在 Johan 的案例中,空间处理交易是有用的
猜你喜欢
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多