Home > Backend Development > PHP Tutorial > 求高手这段json怎样用php解析

求高手这段json怎样用php解析

WBOY
Release: 2016-06-06 20:20:04
Original
1344 people have browsed it

如题,我想解析网易云音乐歌单内容,但是php不熟这段json又太复杂实在是没有解析出来...

地址:http://music.163.com/api/playlist/detail?id=92664101

我想输出nickname和avatarUrl,循环输出歌单里歌的mp3Url,name还有artist里面的picUrl

回复内容:

如题,我想解析网易云音乐歌单内容,但是php不熟这段json又太复杂实在是没有解析出来...

地址:http://music.163.com/api/playlist/detail?id=92664101

我想输出nickname和avatarUrl,循环输出歌单里歌的mp3Url,name还有artist里面的picUrl

拿去玩吧。
演示地址:http://1.wxrain.sinaapp.com/sf-1010000004321535.php

不要跟我说时间怎么是几百秒的,自行转换

求高手这段json怎样用php解析

<code><?php $url = 'http://music.163.com/api/playlist/detail?id=92664101';
$json = file_get_contents($url);
$obj = json_decode($json);

if (! is_object($obj) || $obj->code != 200) {
    exit('fetch data fail');
}
$result = $obj->result;
?>


<meta charset="UTF-8">
<title>netease JSON parse</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href="http://www.bootcss.com/p/flat-ui/css/flat-ui.css" rel="stylesheet">
<link rel="shortcut icon" href="http://a.tbcdn.cn/s/kissy/favicon.ico" type="image/x-icon">


    <div class="container">
        <h1 class="page-header">
<?php echo $result->creator->nickname;?><small>avatarUrl:<?php echo $result->creator->avatarUrl;?></small>
        </h1>

        <div class="row">
  <?php if(count($result->tracks)){?>
  <table class="table table-striped">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>歌曲标题</th>
                        <th>时长</th>
                        <th>歌手</th>
                        <th>mp3Url</th>
                        <th>picUrl</th>
                    </tr>
                </thead>
                <tbody>
      <?php foreach($result->tracks as $key=>$row){?>
          <tr>
                        <th><?php echo $key;?></th>
                        <th>
<?php echo $row->name;?></th>
                        <th>
<?php echo $row->duration/1000;?>s</th>
                        <th>
<?php echo $row->artists[0]->name;?></th>
                        <th><a href="<?php%20echo%20%24row->mp3Url;?>" target="_blank">播放链接</a></th>
                        <th><a href="<?php%20echo%20%24row->album->picUrl;?>" target="_blank">picUrl</a></th>
                    </tr>
      <?php }?>
      </tbody>
            </table>
  <?php }?>
  </div>
    </div>

</code>
Copy after login

php 解析 json 使用 json_decode() 函数即可

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

<code class="php">$json = json_decode(file_get_contens(http://music.163.com/api/playlist/detail?id=92664101),true);
echo $json['result']['creator']['nickname'];//fafatears
echo $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);</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template