首页 > 解决方案 > 将文件存储在 JSON 中以发送到移动应用程序时出错

问题描述

我正在尝试将数组存储在 JSON 中,但是,JSON 文件使用空格或空格字符,并且代码在 JSON 验证器中给了我错误。

以下是我的代码:

require $_SERVER['DOCUMENT_ROOT'].'/config/init.php';
require CLASS_PATH.'user.php';
require CLASS_PATH.'news.php';
require CLASS_PATH.'podcast.php';

$user = new User();

$podcast = new Podcast();

$news = new News();

$userList = $user->getAllUserName();

foreach ($userList as $users) {

$fullname = $users->full_name; 

$profile = $users->image;

$podcastList = $podcast->getUserPodcast($fullname);
$output = [];
if (!empty($podcastList)) {
    $output[] = array(
    'name'  => $fullname,
    'profile'   => $profile,
    'podcast' => $podcastList
);
}

}

$newsList = $news->getAllNews();

if ($newsList) {
$data = array();
foreach ($newsList as $value) {
    $data = array(
        'id'=>$value->id,
        'title'=>$value->title,
        'story'=>html_entity_decode($value->story),
        'image'=>$value->image,
        'added_date'=>$value->added_date,
        'news_category'=>$value->news_category,
        'author'=>$value->author
    );
}
}

$json = array(
'news'  => $data,
'podcast'   => $output
);

echo json_encode($json);

每当我运行这个文件时,它都会给我以下错误:

错误:第 1 行的解析错误:\u0938\ u0930\ u091 ^ 期望 'STRING'、'NUMBER'、'NULL'、'TRUE'、'FALSE'、'{'、'[',得到 'undefined'

我该如何解决?

标签: arraysjson

解决方案


一个可能的答案可能是:

发送header('Content-Type: application/json; charset=utf-8');


推荐阅读