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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题