How to Use php to parse json

Source: Internet
Author: User
Tags php foreach
For example, I want to parse the content of Netease cloud music songs, but php is not familiar with this json section. It is too complicated to parse it... Address: music.163.comapiplaylistdetail? Id92664101 I want to output nickname and avatarUrl, and cyclically output the mp3Url of the song in the song list ,... for example, I want to parse the content of the Netease cloud music song list, but php is not familiar with this json section and it is too complicated to parse it...

Http://music.163.com/api/playlist/detail? Id = 92664101

I want to output nickname and avatarUrl, cyclically output the mp3Url of the song in the song list, and the picUrl In the artist.

Reply content:

For example, I want to parse the content of the Netease cloud music song list, but php is not familiar with this json section and it is too complicated to parse it...

Http://music.163.com/api/playlist/detail? Id = 92664101

I want to output nickname and avatarUrl, cyclically output the mp3Url of the song in the song list, and the picUrl In the artist.

Take it for fun.
Demo address: http://1.wxrain.sinaapp.com/sf-1010000004321535.php

Don't tell me how the time is several hundred seconds, switch by yourself


  Code! = 200) {exit ('fetch data fail ');} $ result = $ obj-> result;?>
  Netease JSON parse
  
  
  
      

Creator-> nickname;?>AvatarUrl: Creator-> avatarUrl;?>

Tracks) {?>

Tracks as $ key => $ row) {?>
# Song title Duration Artist Mp3Url PicUrl
Name;?> Duration/1000;?> S Artists [0]-> name;?> Mp3Url;?> "Target =" _ blank "> playback Link Album-> picUrl;?> "Target =" _ blank "> picUrl

Use the json_decode () function to parse json in php.

Http://www.php.net/manual/zh/function.json-decode.php

$json = json_decode(file_get_contens(http://music.163.com/api/playlist/detail?id=92664101),true);echo $json['result']['creator']['nickname'];//fafatearsecho $json['result']['creator']['avatarUrl'];//http://p4.music.126.net/8H54LZSIvkMK8tqCJZnbTw==/3294136843838189.jpg$mp3 = [];$pic = [];foreach($json['result']['tracks'] as $value){    $mp3[] = $value['mp3Url'];    foreach($value['artists'] as $v) {        $pic[] = $v['picUrl'];    }}var_dump($mp3);var_dump($pic);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.