PHP - 试图让json_encode添加逗号和括号

时间:2015-01-20 19:17:22

标签: php json

我有一个应用程序,它根据JSON文件显示网站列表和一些有关它们的信息。我正在努力使应用程序的用户可以通过Web表单自行添加新的网站列表。

我遇到的问题是,当我使用JSON_ENCODE时,它附加的JSON文件上没有开始或结束括号 - 并且没有逗号分隔每个对象。

如果没有添加这些字符,我的应用程序将无法读取JSON文件。我已经做了很多研究,并且在这个问题上我已经在墙上撞了几天。我是非常新的PHP所以请原谅我,如果已经回答了,但我已经搜索过,没有运气。

这是我的PHP代码:

<?php

if(isset($_POST['age']) && isset($_POST['id']) && isset($_POST['image']) && isset($_POST['name']) && isset($_POST['snippet0']) && isset($_POST['snippet1']) 
  && isset($_POST['snippet3']) && isset($_POST['snippet4']) && isset($_POST['snippet5']) && isset($_POST['snippet6'])) {

  if(empty($_POST['age']) || empty($_POST['id']) || empty($_POST['image']) || empty($_POST['name']) || empty($_POST['snippet0']) || empty($_POST['snippet1']) 
    || empty($_POST['snippet2']) || empty($_POST['snippet3']) || empty($_POST['snippet4']) || empty($_POST['snippet5']) || empty($_POST['snippet6'])) {
    echo 'All fields are required';
  }
  else {
    $postArray = array(
      "age" => $_POST['age'],
      "id" => $_POST['id'],
      "image" => $_POST['image'],
      "name" => $_POST['name'],
      "snippet0" => $_POST['snippet0'],
      "snippet1" => $_POST['snippet1'],
      "snippet2" => $_POST['snippet2'],
      "snippet3" => $_POST['snippet3'],
      "snippet4" => $_POST['snippet4'],
      "snippet5" => $_POST['snippet5'],
      "snippet6" => $_POST['snippet6']
    );


$jsondata = json_encode ( $postArray, JSON_PRETTY_PRINT );


$file = 'data/formdata.json';
if(file_put_contents( $file, $jsondata, FILE_APPEND )) echo 'Data saved';
else echo 'Unable to save data';
  }
}
else echo 'Form fields not submitted';
?>

以下是我得到的JSON输出示例:

{
    "age": "1",
    "id": "bob",
    "image": "bob.png",
    "name": "Bob.com",
    "snippet0": "sub.bob.com",
    "snippet1": "sub.bob.com",
    "snippet2": "sub.bob.com",
    "snippet3": "sub.bob.com",
    "snippet4": "sub.bob.com",
    "snippet5": "sub.bob.com",
    "snippet6": "sub.bob.com"
}{
    "age": "1",
    "id": "bob",
    "image": "bob.png",
    "name": "Bob.com",
    "snippet0": "sub.bob.com",
    "snippet1": "sub.bob.com",
    "snippet2": "sub.bob.com",
    "snippet3": "sub.bob.com",
    "snippet4": "sub.bob.com",
    "snippet5": "sub.bob.com",
    "snippet6": "sub.bob.com"
}

最后,我想要一个JSON输出的例子:

[{
    "age": "1",
    "id": "bob",
    "image": "bob.png",
    "name": "Bob.com",
    "snippet0": "sub.bob.com",
    "snippet1": "sub.bob.com",
    "snippet2": "sub.bob.com",
    "snippet3": "sub.bob.com",
    "snippet4": "sub.bob.com",
    "snippet5": "sub.bob.com",
    "snippet6": "sub.bob.com"
},{
    "age": "1",
    "id": "bob",
    "image": "bob.png",
    "name": "Bob.com",
    "snippet0": "sub.bob.com",
    "snippet1": "sub.bob.com",
    "snippet2": "sub.bob.com",
    "snippet3": "sub.bob.com",
    "snippet4": "sub.bob.com",
    "snippet5": "sub.bob.com",
    "snippet6": "sub.bob.com"
}]

2 个答案:

答案 0 :(得分:0)

首先,从文件中读取

$file_content = json_decode( file_get_contents('data/formdata.json') );

然后添加数据

$file_content []= 
    array(
      "age" => $_POST['age'],
      "id" => $_POST['id'],
      "image" => $_POST['image'],
      "name" => $_POST['name'],
      "snippet0" => $_POST['snippet0'],
      "snippet1" => $_POST['snippet1'],
      "snippet2" => $_POST['snippet2'],
      "snippet3" => $_POST['snippet3'],
      "snippet4" => $_POST['snippet4'],
      "snippet5" => $_POST['snippet5'],
      "snippet6" => $_POST['snippet6']
    );

最后只需将内容放回文件

$file_content = json_encode ( $file_content, JSON_PRETTY_PRINT );
file_put_contents( 'data/formdata.json', $file_content );

答案 1 :(得分:0)

这很简单,只需在json_encode功能

中的第二个参数中选择
JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE

示例:

json_encode($date,JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)