如何在php中将jsonarray转换为jsonobject并另存为json文件

如何在php中将jsonarray转换为jsonobject并另存为json文件,php,javascript,json,Php,Javascript,Json,结果 require_once("conect.php"); $sqlString="SELECT * FROM articles;"; $response = array(); $posts = array(); $query=mysql_query($sqlString) or die (mysql_error()); while ($row=mysql_fetch_array($query)){ $title =$row["title"]; $aut

结果

require_once("conect.php");
$sqlString="SELECT * FROM articles;";
$response = array();
$posts = array();
$query=mysql_query($sqlString) or die (mysql_error());
    while ($row=mysql_fetch_array($query)){
        $title =$row["title"];
        $author =$row["author"];
        $article =$row["article"];
        $posts[] = array('title'=> $title, 'author'=> $author, 'article'=> $article);

    }
$response['posts'] = $posts;

$fp = fopen('json\results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);`

我希望它另存为jsonobject

将mysql结果作为对象获取。不要费心转换它

这就是你要找的。

我找到了答案

$json_data = json_encode($response);
$filename ="yourfilename.json";
header('Content-type: application/json');
header('Content-Disposition: attachment; filename='.$filename);
echo $json_data ;
`
产生:

文身是一种身体修饰形式,通过在皮肤的真皮层插入不褪色的墨水来改变色素。},}标题:“,”狗“,”作者“:”拉克马尔“,”文章“,”家养狗(熟悉犬狼疮)[2][3]是灰狼(犬狼疮)的一个亚种“家养狗”一词通常用于家养和野生品种。“猫”,“作者”:“chamikara”,“文章”家猫[1][2](猫科动物[2]或猫科动物[4])是一种小型的、通常是毛茸茸的家养食肉哺乳动物。当作为室内宠物饲养时,它通常被称为家猫,[6]或者当不需要区分“汽车”、“作者”、“lakmal”、“文章”时,它被称为猫“汽车、汽车、机动车辆或汽车是一种用于运送乘客的轮式机动车辆,也装有自己的发动机或发动机。该术语的大多数定义都规定,汽车主要在道路上行驶,并具有se“},}”


`$title,'author'=>$author,'article'=>$article)


?>`

尝试使用mysqli,mysqli在PHP 5.5.0中已被弃用!可能重复到@Nomi我想另存为json文件,而不是json对象文件的数组我从上面的代码中得到了这些------------------------------------------{“posts”:[{“title”:“tatoo”,“author”:“lakmal”,“article”:“A too is A.”,{“title”:“dog”,“author”:“laal”,“article”:“dotic dog”},{标题:“‘猫’,‘作者’:‘查拉’,‘文章’:‘dstic’,{‘标题’:‘汽车’,‘作者’:‘laal’,‘文章’:‘aumobile’,}}我想转换成这样{‘标题’:‘tatoo’,‘作者’:‘laal’,‘文章’:‘doic狗’,{‘标题’:‘猫’,‘作者’:‘chaara’,‘文章’:‘Doistic’,{标题:““汽车”、“作者”:“laal”、“article”:“An aule”}如果您使用的是任何客户端或服务器脚本,为什么不先阅读文件并生成一个变量,然后生成一个
对象
mysql_fetch_object
$json_data = json_encode($response);
$filename ="yourfilename.json";
header('Content-type: application/json');
header('Content-Disposition: attachment; filename='.$filename);
echo $json_data ;
<?php
        require_once("conect.php");
        $jsonData="[{";
        $sqlString="SELECT * FROM articles;";
        $query=mysql_query($sqlString) or die (mysql_error());
            while ($row=mysql_fetch_array($query)){
                $title =$row["title"];
                $author =$row["author"];
                $article =$row["article"];
                $jsonData.='"title":"'.$title.'","author":"'.$author.'","article"'.$article.'"},';      
                $jsonData.="{";         
            }
        $jsonData = chop ($jsonData, ",");
        $jsonData.="]";


        $fp = fopen('results.json', 'w');
        fwrite($fp,json_encode($jsonData));
        fclose($fp) 

?>`
        }
    $response = $posts;

    $fp = fopen('results.json', 'w');
    fwrite($fp, json_encode($response));
    fclose($fp);