1

PHP を使用して次の RAW データをループし、この結果を得るにはどうすればよいですか。

結果:

 NAME1 - SONG1
 NAME2 - SONG2
 NAME3 - SONG3

生データ:

{"artist":"NAME1","title":"SONG1","album":null,"royaltytrackid":null,"url":null,"image":"test1.jpg","time":1380910069,"localtime":"11:07 AM"},
{"artist":"NAME2","title":"SONG2","album":null,"royaltytrackid":null,"url":null,"image":"test2.jpg","time":1380909866,"localtime":"11:04 AM"},  
{"artist":"NAME2","title":"SONG3","album":null,"royaltytrackid":null,"url":null,"image":"test3.jpg","time":1380909864,"localtime":"11:04 AM"}
4

1 に答える 1

5

[入力データにリストの周囲が欠けているように見える]ため、JSON として解析する前に、それらを追加する必要があります。

$rawData = <<<EOL
{"artist":"NAME1","title":"SONG1","album":null,"royaltytrackid":null,"url":null,"image":"test1.jpg","time":1380910069,"localtime":"11:07 AM"},
{"artist":"NAME2","title":"SONG2","album":null,"royaltytrackid":null,"url":null,"image":"test2.jpg","time":1380909866,"localtime":"11:04 AM"},  
{"artist":"NAME2","title":"SONG3","album":null,"royaltytrackid":null,"url":null,"image":"test3.jpg","time":1380909864,"localtime":"11:04 AM"}
EOL;

$data = json_decode("[" . $rawData . "]");

foreach ($data as $item) {
    echo $item->artist, " - ", $item->title, "\n";
}

出力:

NAME1 - SONG1
NAME2 - SONG2
NAME2 - SONG3
于 2013-10-05T11:40:46.220 に答える