php 使用php编码json?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7531965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:53:25  来源:igfitidea点击:

encode json using php?

phpjson

提问by osos

I want to get json with php encode function like the following

我想使用如下所示的 php 编码功能获取 json

<?php 
  require "../classes/database.php";

  $database = new database();
  header("content-type: application/json");
  $result = $database->get_by_name($_POST['q']);   //$_POST['searchValue']

  echo '{"results":[';
  if($result)
  {
     $i = 1;
     while($row = mysql_fetch_array($result))
     {
        if(count($row) > 1) 
        {
           echo json_encode(array('id'=>$i, 'name' => $row['name']));
           echo ",";
        }
        else 
        {
           echo json_encode(array('id'=>$i, 'name' => $row['name']));
        }
        $i++; 
     }
  }
  else
  {
     $value = "FALSE";
     echo json_encode(array('id'=>1, 'name' => ""));  // output the json code
  }

  echo "]}";

i want the output json to be something like that

我希望输出 json 是这样的

{"results":[{"id":1,"name":"name1"},{"id":2,"name":"name2"}]}

but the output json is look like the following

但输出 json 如下所示

{"results":[{"id":1,"name":"name1"},{"id":2,"name":"name2"},]}

As you realize that there is comma at the end, i want to remove it so it can be right json syntax, if i removed the echo ",";when there's more than one result the json will generate like this {"results":[{"id":1,"name":"name1"}{"id":2,"name":"name2"}]}and that syntax is wrong too

当您意识到末尾有逗号时,我想将其删除,以便它可以是正确的 json 语法,如果我删除了echo ",";当有多个结果时 json 会像这样生成{"results":[{"id":1,"name":"name1"}{"id":2,"name":"name2"}]}并且该语法也是错误的

Hope that everybody got what i mean here, any ideas would be appreciated

希望每个人都明白我的意思,任何想法将不胜感激

回答by Jeff Lambert

If I were you, I would not json_encodeeach individual array, but merge the arrays together and then json_encodethe merged array at the end. Below is an example using 5.4'sshort array syntax:

如果我是你,我不会json_encode每个单独的数组,而是将数组合并在一起,然后json_encode在最后合并数组。下面是一个使用5.4 的短数组语法的例子:

$out = [];
while(...) {
  $out[] = [ 'id' => $i, 'name' => $row['name'] ];
}
echo json_encode($out);

回答by Marc B

Do the json_encoding as the LAST step. Build your data structure purely in PHP, then encode that structure at the end. Doing intermediate encodings means you're basically building your own json string, which is always going to be tricky and most likely "broken".

将 json_encoding 作为最后一步。完全用 PHP 构建您的数据结构,然后在最后对该结构进行编码。进行中间编码意味着您基本上是在构建自己的 json 字符串,这总是很棘手,而且很可能会“损坏”。

$data = array();
while ($row = mysql_fetch_array($result)) {
   $data[] = array('id'=>$i, 'name' => $row['name']);
}
echo json_encode($data);

回答by Spudley

build it all into an array first, then encode the whole thing in one go:

首先将其全部构建到一个数组中,然后一次性编码整个内容:

$outputdata = array();
while($row = mysql_fetch_array($result)) {
    $outputdata[] = $row;
}

echo json_encode($outputdata);

回答by osos

Here is a solution that i came with

这是我带来的解决方案

 $i = 1; 
 $array = array(); 
 while($row = mysql_fetch_array($result)) 
 { 
 $array[] = json_encode(array('id'=>$i, 'name' => $row['name'])); 
 $i++; 
 } 
 echo implode(',', $array); // Take output array glue it with the  

This will put the json into an array and then implode it with glue (,) outputing the following {"results":[{"id":1,"name":"maadi"},{"id":2,"name":"monofiya"}]}without the need to do the array first then pass it to the json_encode() function

这会将 json 放入一个数组中,然后使用胶水 (,) 对其进行内爆,输出以下内容,{"results":[{"id":1,"name":"maadi"},{"id":2,"name":"monofiya"}]}而无需先执行该数组,然后将其传递给 json_encode() 函数

回答by Merianos Nikos

Just change that lines

只需改变那几行

echo json_encode(array('id'=>$i, 'name' => $row['name']));
echo ",";

To these

对这些

echo ",";
echo json_encode(array('id'=>$i, 'name' => $row['name']));