douju2053 2017-11-05 14:53
浏览 76

如何解析json并将其保存在数据库中?

first sorry for my bad english. I have a POST request with a json inside and i would save it in my database which has adjacency List model . It composed with 2 columns: name and parent_id, How can i parse this json using php, then save it in my database. Any suggestions ? Thanks.

My json:

{
  "name": "Category 1",

  "children": [
    {
      "name": "Category 1.1",
      "children": [
        {
          "name": "Category 1.1.1",
          "children": [
            {
              "name": "Category 1.1.1.1"
            },
            {
              "name": "Category 1.1.1.2"
            },
            {
              "name": "Category 1.1.1.3"
            }
          ]

        },

        {
          "name": "Category 1.1.2",
          "children": [
            {
              "name": "Category 1.1.2.1"
            },
            {
              "name": "Category 1.1.2.2"
            },
            {
              "name": "Category 1.1.2.3"
            }
          ]
        }
      ]
    },
    {
      "name": "Category 1.2",
      "children": [
        {
          "name": "Category 1.2.1"
        },
        {
          "name": "Category 1.2.2",
          "children": [
            {
              "name": "Category 1.2.2.1"
            },
            {
              "name": "Category 1.2.2.2"
            }
          ]
        }
      ]
    }
  ]
}
  • 写回答

3条回答 默认 最新

  • dream0614 2017-11-05 15:34
    关注

    To parse the JSON, try using PHP's json_decode function. http://php.net/manual/en/function.json-decode.php

    I can't give you specifics on how to store it in the database without knowing what type of database it is, but you probably should use PHP's PDO (PHP Data Object) API. (http://php.net/manual/en/book.pdo.php) It lets you prepare and execute queries/insertions/commands just like you can the database's shell.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器