【发布时间】:2015-11-01 03:22:15
【问题描述】:
我有一个 JSON 文件,我想通过 PHP 访问其中的内容。问题是访问 JSON 文件中的数组。此站点上建议的其他方法似乎不起作用。 JSON 结构的示例在底部。这里的 PHP 代码是我打开和关闭 PHP 标记之间的唯一 PHP 代码。
此 PHP 代码有效。我正在访问不是数组的东西。
$jsondata = file_get_contents('BFZ.json');
$data = json_decode($jsondata, true);
$id = $data['name'];
echo $id;
这不起作用。我正在尝试访问 JSON 文件中“卡片”数组(对象?)的“名称”部分。
$jsondata = file_get_contents('BFZ.json');
$data = json_decode($jsondata, true);
$id = $data['cards']['name'];
echo $id;
这也行不通:
$id = $data['cards']['name'][0];
带有示例信息的 JSON 文件的结构:
"name" : "Nemesis",
"code" : "NMS",
"gathererCode" : "NE",
"oldCode" : "NEM",
"magicCardsInfoCode" : "ne",
"releaseDate" : "2000-02-14",
"border" : "black",
"type" : "expansion",
"block" : "Masques",
"onlineOnly" : false,
"booster" : [ "rare", ... ],
"cards" : [ {}, {}, {}, ... ]
带有示例信息的 JSON 文件的“卡片”数组(对象?)的结构:
"name" : "Sen Triplets",
"manaCost" : "{2}{W}{U}{B}",
"cmc" : 5,
"colors" : ["White", "Blue", "Black"],
"type" : "Legendary Artifact Creature — Human Wizard",
"supertypes" : ["Legendary"],
"types" : ["Artifact", "Creature"],
"subtypes" : ["Human", "Wizard"],
"rarity" : "Mythic Rare",
"text" : "At the beginning of your upkeep, choose target opponent.
This turn, that player can't cast spells or activate
abilities and plays with his or her hand revealed.
You may play cards from that player's hand this turn.",
"flavor" : "They are the masters of your mind.",
"artist" : "Greg Staples",
"number" : "109",
"power" : "3",
"toughness" : "3",
"layout" : "normal",
"multiverseid" : 180607,
"imageName" : "sen triplets",
"id" : "3129aee7f26a4282ce131db7d417b1bc3338c4d4"
我从这里得到了 JSON 文件:http://mtgjson.com/。该文件引用了纸牌游戏 Magic: the Gathering。我使用 PHP 是因为我的目的是最终将数据加载到 MySQL 数据库中。
【问题讨论】: