私はXMLから解析している書籍のリストを取得しようとしていますが、JSONとして出力したいと思います。SimpleXMLを使用してXMLからJSONを構築する際の問題
私は、JSONの形式はになりたい:しかし、結果は次のように出ている
[
"1" : {
"Title": "Sidemen: The Book",
"ISBN": "1473648165",
"Rating": "4.5"
},
...
]
:
[
{
"title":{
"0":"Sidemen: The Book"
},
"ISBN":{
"0":"1473648165"
}
},
{
"title":{
"0":"DanTDM: Trayaurus and the Enchanted Crystal"
},
"ISBN":{
"0":"1409168395"
}
},
{
"title":{
"0":"Pok\u00e9mon Sun & Pok\u00e9mon Moon: The Official Strategy Guide"
},
"ISBN":{
"0":"1911015109"
}
},
{
"title":{
"0":"Guinness World Records 2017 Gamer's Edition"
},
"ISBN":{
"0":"1910561398"
}
},
{
"title":{
"0":"Minecraft: Blockopedia: An Official Minecraft Book from Mojang"
},
"ISBN":{
"0":"1405273534"
}
},
{
"title":{
"0":"Final Fantasy XV - The Complete Official Guide - Collector's Edition"
},
"ISBN":{
"0":"1911015001"
}
},
{
"title":{
"0":"Harry Potter: Collectible Quidditch Set"
},
"ISBN":{
"0":"076245945X"
}
},
{
"title":{
"0":"Pok\u00e9mon Go The Unofficial Field Guide: Tips, tricks and hacks that will help you catch them all!"
},
"ISBN":{
"0":"1783707712"
}
},
{
"title":{
"0":"Minecraft 2017 Annual (by GamesMaster) (2017 Annuals)"
},
"ISBN":{
"0":"0995495025"
}
},
{
"title":{
"0":"World of Warcraft The Official Cookbook"
},
"ISBN":{
"0":"1785654349"
}
}
]
私はこれである理由を見つけ出すように見えることはできません私が欲しいものをやっていない(probsは私がノブだから)。このようにPHPで生成されます:
$bookList = array();
$id = 0;
foreach ($parsed_xml->Items->Item as $item) {
$response = file_get_contents($GoodReadsProductLink);
$parsed_xml = simplexml_load_string($response);
$currentBook = array(
"title" => $item->ItemAttributes->Title,
"ISBN" => $item->ItemAttributes->ISBN,
"Rating" => $item->ItemAttributes->Rating
);
$bookList[$id] = $currentBook;
$id++;
}
$jsonOutput = json_encode($bookList);
var_dump($jsonOutput);
誰でも問題を見て、JSON出力を正しくフォーマットするのに手伝ってください。
形式が有効なJSONではありません...あなたが現在取得している何が悪いのを教えてできますか? –
また、これらのsimplexml要素を文字列にキャストしたいと思うかもしれません。例えば。 '" title "=>(文字列)$ item-> ItemAttributes-> Title'である。もう一つは、 'Rating'が実際には存在しないように見えますか? –
@ john-stirlingそれは有効なJSONではありませんか?私は正しい方法でやっているのですか? id: – JamesG