如何在PHP中创建json文件 [英] How to create a json file in PHP

查看:195
本文介绍了如何在PHP中创建json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从头开始创建一个看起来像这样的JSON文件

I am needing to create a JSON file from scratch that will look like this

{
 "results": {
  "course": "CC167",
  "books": {
   "book": [
    {
      "-id": "585457",
      "-title": "Beginning XNA 20 game programming : from novice to professional",
      "-isbn": "1590599241",
      "-borrowedcount": "16"
    },
    {
      "-id": "325421",
      "-title": "Red Hat Linux 6",
      "-isbn": "0201354373",
      "-borrowedcount": "17"
    },
    {
      "-id": "424317",
      "-title": "Beginner's guide to darkBASIC game programming",
      "-isbn": "1592000096",
      "-borrowedcount": "46"
    },
    {
      "-id": "437390",
      "-title": "Objects first with Java : a practical introduction using BlueJ",
      "-isbn": "0131249339",
      "-borrowedcount": "89"
    },
    {
      "-id": "511094",
      "-title": "Objects first with Java : a practical introduction using BlueJ",
      "-isbn": "2006044765",
      "-borrowedcount": "169"
    }
   ]
  }
 }
}

这是我用来制作PHP的PHP,因此希望这不是一个大的飞跃,但是我对如何从头开始在PHP中制作JSON对象一无所知,只知道如何制作这样的东西然后保存自己将其作为JSON

This is the PHP i used to make that, so hopefully it isn't a big jump, but I just can't anything on how to make JSON objects in PHP from scratch, only how to make something like this then save it as a JSON yourself

$y = 1;
    $json = "{";
    $json = $json . "\"results\": {";
    $json = $json . "\"course\": \"$cc\",";
    $json = $json . "\"books\": {";
    $json = $json . "\"book\": [";
    foreach ($my_array as $counter => $bc) {
        $json = $json . "{";
        $json = $json . "\"-id\": \"$id[$counter]\",";
        $json = $json . "\"-title\": \"$title[$counter]\",";
        $json = $json . "\"-isbn\": \"$isbn[$counter]\",";
        $json = $json . "\"-borrowedcount\": \"$borrowedcount[$counter]\"";
        $json = $json . "}";
        if ($x != $y) $json = $json .  ",";
        $json = $json . "";
        $y++;
    }
    $json = $json . "]";
    $json = $json . "}";
    $json = $json . "}";
    $json = $json . "}";
    echo $json;

推荐答案

您可以使用例如,这将生成类似于您上面的json的内容(略有减少)

For example this would generate something similar to your json above (slightly cut down)

$data = array(
  "results" => array(
    "course" => "CC167",
    "books" => array(
      "book" =>
      array(
        array(
          "-id" => "585457",
          "-title" => "Beginning XNA 20 game programming : from novice to professional",
          "-isbn" => "1590599241",
          "-borrowedcount" => "16"
        ),
        array(
          "-id" => "325421",
          "-title" => "Red Hat Linux 6",
          "-isbn" => "0201354373",
          "-borrowedcount" => "17"
        )
      )
    )
  )
);
echo json_encode($data);

尝试通过手动字符串连接生成json(就像您当前的代码一样)是个坏主意,因为它很难避免语法错误,并且您需要转义json的动态部分. json_encode会自动为您转义.

Trying to generate json by manual string concatenation (as in your current code) is bad idea as its hard to avoid syntax errors and you need to be escaping dynamic parts of the json. json_encode automatically does that escaping for you.

这篇关于如何在PHP中创建json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆