arrays - 将文件存储在 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'
我该如何解决?
解决方案
一个可能的答案可能是:
发送header('Content-Type: application/json; charset=utf-8');
推荐阅读
- java - android firebase数据库如何从子节点检索所有数据
- android - Gradle 构建“错误:资源 android:style/TextAppearance.Material.Widget.Button.Borderless.Colored 未找到。”
- reactjs - 在 react redux saga 中处理同步
- api - 垃圾邮件的 GMail API 调用返回“无效的 id 值”
- javascript - 如何在 Rails 关联模型中映射嵌套对象?
- r - 带有刻面和填充 R 的小提琴图中的箱线图
- ios - 如何在 `UIImageView` 中加载动画 webp 图像
- sustainsys-saml2 - SustainSys.Saml2 请求长度标头太长
- amazon-web-services - 在 IAM 策略中使用前缀为 aws: 的标签
- c# - C#服务HTTP请求忽略自定义超时值