首页 > 解决方案 > 为什么我的 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! :|"
      },

标签: phpjsondecode

解决方案


您需要使用$this为您的类变量赋值$lieder。如果您希望它返回关联数组而不是对象,也应该传递true给。json_decodehttps://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);

推荐阅读