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.

    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题