2016-11-28 13 views
1

私は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出力を正しくフォーマットするのに手伝ってください。

+2

形式が有効なJSONではありません...あなたが現在取得している何が悪いのを教えてできますか? –

+1

また、これらのsimplexml要素を文字列にキャストしたいと思うかもしれません。例えば。 '" title "=>(文字列)$ item-> ItemAttributes-> Title'である。もう一つは、 'Rating'が実際には存在しないように見えますか? –

+0

@ john-stirlingそれは有効なJSONではありませんか?私は正しい方法でやっているのですか? id: – JamesG

答えて

1

CastSimpleXmlElementオブジェクトto stringを使用し、JSON_FORCE_OBJECTオプションを使用します。

例。あなたが希望している

$xml = <<<'XML' 
<Items> 
    <Item Title="Book Title 1" ISBN="ISBN 1" Rating="4.5"/> 
    <Item Title="Book Title 2" ISBN="ISBN 2" Rating="5.0"/> 
</Items> 
XML; 
$doc = simplexml_load_string($xml); 

$id = 0; 
$books = []; 
foreach ($doc as $item) { 
    if (! $attr = $item->attributes()) { 
    continue; 
    } 

    if (empty($attr['Title']) || empty($attr['ISBN']) || empty($attr['Rating'])) { 
    continue; 
    } 

    $books[++$id] = [ 
    'title' => (string)$attr['Title'], 
    'ISBN' => (string)$attr['ISBN'], 
    'Rating' => (string)$attr['Rating'], 
    ]; 
} 

echo $json = json_encode($books, JSON_FORCE_OBJECT | JSON_PRETTY_PRINT); 

出力

{ 
    "1": { 
     "title": "Book Title 1", 
     "ISBN": "ISBN 1", 
     "Rating": "4.5" 
    }, 
    "2": { 
     "title": "Book Title 2", 
     "ISBN": "ISBN 2", 
     "Rating": "5.0" 
    } 
} 
+0

の下にリンクされているすべての本がほんの少しの微妙な調整で動作するようにしたい。ただし、タイトルに '/'が含まれている場合、出力は '\/\ /'として追加されます。私はstr_replace( "\ /"、 "/"、$ title)を試しましたが、何もしません。 – JamesG

+0

@JamesG、JSON_UNESCAPED_SLASHESオプションを追加 –

関連する問題