在php中将json转换为字符串

时间:2017-07-15 00:46:00

标签: php json

我想将这个json转换为php中的字符串,我希望它只返回标题和描述。

我的网址上的json输出代码:

 {"play-list":[{"id":"48","title":"Sound Remedy \u0026 Illenium - Spirals (feat. King Deco)","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Sound Remedy \u0026 Illenium - Spirals (feat. King Deco).mp3_calitone.com.mp3","tag":"jazz,","art":"1644246783images (15).jpg_www.calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"5800450","download":"1","time":"2017-07-14 08:15:22","public":"1","likes":"0","downloads":"0","views":"215"},{"id":"385","title":"Natalie Imbruglia - Perfectly","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Perfectly_calitone.com.mp3","tag":"Pop","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Perfectly_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 20:09:08","public":"1","likes":"0","downloads":"0","views":"177"},{"id":"384","title":"Natalie Imbruglia - Shiver","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Shiver_calitone.com.mp3","tag":"blues","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Shiver_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 20:04:08","public":"1","likes":"0","downloads":"0","views":"144"},{"id":"383","title":"Natalie Imbruglia - Torn","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Torn_calitone.com.mp3","tag":"blues","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Torn_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 19:58:38","public":"1","likes":"2","downloads":"2","views":"213"}],"success":1} 

这是我的功能如下:

function ct_playlist_data($id) {

    $jsonData = json_decode(file_get_contents('http://www.calitunes.com/android_543ASBD/playlist.php?playlists=yes&listid=6'));
            $tracks = $jsonData->{'play-list'};
            //$success = $value->{'success'};
            echo var_dump($tracks);

}

我被困在这里。

提前谢谢你!

3 个答案:

答案 0 :(得分:4)

基于您的JSON的示例

$array = json_decode($json, true);

foreach ($array['play-list'] as $val) {
  echo $val['title'] . $val['description'] . '<br>';
}
  

SEE DEMO HERE

答案 1 :(得分:0)

试试此代码Demo is here

<?php 

$jsonData = '{"play-list":[{"id":"48","title":"Sound Remedy \u0026 Illenium - Spirals (feat. King Deco)","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Sound Remedy \u0026 Illenium - Spirals (feat. King Deco).mp3_calitone.com.mp3","tag":"jazz,","art":"1644246783images (15).jpg_www.calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"5800450","download":"1","time":"2017-07-14 08:15:22","public":"1","likes":"0","downloads":"0","views":"215"},{"id":"385","title":"Natalie Imbruglia - Perfectly","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Perfectly_calitone.com.mp3","tag":"Pop","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Perfectly_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 20:09:08","public":"1","likes":"0","downloads":"0","views":"177"},{"id":"384","title":"Natalie Imbruglia - Shiver","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Shiver_calitone.com.mp3","tag":"blues","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Shiver_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 20:04:08","public":"1","likes":"0","downloads":"0","views":"144"},{"id":"383","title":"Natalie Imbruglia - Torn","description":"","uid":"9","first_name":"kings ","last_name":"men","name":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Torn_calitone.com.mp3","tag":"blues","art":"Natalie_Imbruglia_-_Natalie_Imbruglia_-_Torn_calitone.com.jpg","buy":"","record":"","release":"0000-00-00","license":"0","size":"0","download":"1","time":"2017-07-10 19:58:38","public":"1","likes":"2","downloads":"2","views":"213"}],"success":1}';
$decode = json_decode($jsonData,true);

foreach($decode['play-list'] as $d){
  echo $d['title'];
  echo $d['description'];
}

?>      

答案 2 :(得分:0)

// $json = your json    
$json = json_decode($json,true);    
$title =[];
foreach($json['play-list'] as $value) {    
    array_push($title,$value['title']);    
}

echo implode('',$title);

echo json_encode($title);

可以工作吗?