php - 为什么我的 JSON 在使用解码时没有在我的 PHP 脚本中被解码?
问题描述
我正在用 PHP 重写我的 Java 程序,但几乎没有使用 PHP 的经验,所以我将代码放在一起并即时通知自己,但我已经坚持解码我的文件(这在我的 Java 版本中运行良好我在哪里使用 GSON 库进行解码)并且真的不知道在哪里寻找我的问题我有这个非常基本的代码:
public $lieder;
public function convertJSONtoARRAY($newJSONFile)
{
$stringJsonFileContents = file_get_contents("$newJSONFile");
$lieder = json_decode($stringJsonFileContents);
}
和这个 :
<?php
include ('LiederEditor.php');
include ('Index.json');
$editor = new LiederEditor();
$editor -> convertJSONtoARRAY("Index.json");
echo var_dump("$editor->lieder");
?>
现在更清洁的版本:
public $lieder;
public function convertJSONtoARRAY($newJSONFile)
{
$stringJsonFileContents = file_get_contents("$newJSONFile");
$this ->lieder = json_decode($stringJsonFileContents,true);
}
和
<?php
include ('LiederEditor.php');
$editor = new LiederEditor();
$editor -> convertJSONtoARRAY("Index.json");
var_dump($editor->lieder);
?>
JSON:
{
"id": 1,
"number": "A01",
"name": "Gaudeamus Igitur, Kindleben",
"slug": "gaudeamus-igitur",
"language": "lat",
"category": "Traditionell",
"year": 1781,
"position": 10010,
"url": "https://beta.acel.lu/group/songbook/gaudeamus-igitur",
"update_time": 1504601629,
"paragraphs": [
{
"id": 1660,
"type": "normal",
"content": "|: Gaudeamus igitur\niuvenes dum sumus :|\npost iucundam iuventutem,\npost molestam senectutem,\n|: nos habebit humus! :|"
},
{
"id": 1661,
"type": "normal",
"content": "|: Ubi sunt qui ante nos\nin mundo fuere? :|\nvadite ad superos\ntransite ad inferos\n|: ubi iam fuere. :|"
},
{
"id": 1662,
"type": "normal",
"content": "|: Vita nostra brevis est,\nbrevi finietur, :|\nvenit mors velociter,\nrapit nos atrociter\n|: nemini parcetur! :|"
},
{
"id": 1663,
"type": "normal",
"content": "|: Vivat academia,\nvivant professores! :|\nvivat membrum quodlibet,\nvivant membra quaelibet,\n|: semper sint in flore! :|"
},
{
"id": 1664,
"type": "normal",
"content": "|: Vivant omnes virgines\nfaciles, formosae, :|\nvivant et mulieres,\ntenerae, amabiles,\n|: bonae, laboriosae :|"
},
{
"id": 1665,
"type": "normal",
"content": "|: Vivat et res publica,\net qui illam regit! :|\nvivat nostra civitas,\nmaecenatum caritas,\n|: quae nos hic protegit! :|"
},
解决方案
您需要使用$this
为您的类变量赋值$lieder
。如果您希望它返回关联数组而不是对象,也应该传递true
给。json_decode
见https://www.php.net/manual/en/function.json-decode.php
public $lieder;
public function convertJSONtoARRAY($newJSONFile)
{
$stringJsonFileContents = file_get_contents($newJSONFile);
$this->lieder = json_decode($stringJsonFileContents, true);
}
您也不需要在变量周围加上引号$newJSONFile
其他几件事:
include('Index.json')
如果您稍后通过 阅读它,则不需要file_get_contents
,并且在 var_dump 时不应使用引号$editor->lieder
。正如@brombeer 指出的那样,您也不需要echo
以下结果var_dump
:
<?php
include ('LiederEditor.php');
$editor = new LiederEditor();
$editor -> convertJSONtoARRAY("Index.json");
var_dump($editor->lieder);
推荐阅读
- sql - SQL Server 中除以 0
- python - 如果我在 CMD 上运行 python 文件,则不会创建 csv 文件
- python - 单独替换商标符号 (™)
- postgresql - 将 csv 复制到容器中以在 init.sql 中使用
- javascript - “fullscreenchange”事件与每个浏览器都不兼容(寻找 Vanilla Javascript 修复)
- excel - 如何定义一个变量等于最后一张表的名称(VBA)?
- html - 使子选项卡溢出选项卡栏(使用 flexbox)
- swift - SwiftUI:致命错误:访问状态
外部视图.body" - java - 在反应式编程中使用番石榴条纹的正确方法是什么?
- java - ShakeService.java 通过意图调用后不打印日志消息