解析嵌套的JSON以检索嵌套的数组值 [英] Parsing nested JSON to retrieve nested array values

查看:45
本文介绍了解析嵌套的JSON以检索嵌套的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此Json中获取一些特定领域.我已经设法在第一级中检索了这些,但是我需要从数据字段中获取一些信息.我想获取以下数据字段的数组:

I am trying to get some specific fields out of this Json. I have already managed to retrieve the ones in the first level but I need to get some out of the data field. I would like to get an array of the following data fields:

data = [(data artist id, data artist id name, and data rank), (data artist id, data artist id name, and data rank)...] 

例如:

data = [(ed61fe981f9143fe82536a0e5e9836f7, Rihanna, 1), (668dfb9383684b79ba603605db21ac51, PSY, 2)..]

有什么想法吗?

Json在下面:

{
" response": {
"class": "chart",
"data": [
    {
        "artist": {
            "class": "artist",
            "id": "ed61fe981f9143fe82536a0e5e9836f7",
            "musicbrainz": "73e5e69d-3554-40d8-8516-00cb38737a1c",
            "name": "Rihanna"
        },
        "rank": 1,
        "value": 42437.6397
    },
    {
        "artist": {
            "class": "artist",
            "id": "668dfb9383684b79ba603605db21ac51",
            "musicbrainz": "f99b7d67-4e63-4678-aa66-4c6ac0f7d24a",
            "name": "PSY"
        },
        "rank": 2,
        "value": 21562.2685
    },

   … goes up to about 200 items
   ],

     "end_time": 1358553600,
    "id": "396c5b836ce74200b2b5b8ba1df28956",
     "name": "high_flyers_plays_total",
     "next_id": null,
     "now_id": "b857276b34cf488f9a934765c3281af7",
     "period": 86400,
     "previous_id": "cb566393058c4ddfa0b957063fbdc2e3",
     "start_time": 1358467200
     },
   "success": true
  }

这是我的php代码:

<?php


  //Chart json url
  $url = "http://website.com";

   function get_json($url)
   {
      $ch = curl_init();

      //Very short time to get json so long timeout not needed
       $timeout = 5;
       curl_setopt($ch,CURLOPT_URL,$url);
       curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
       curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
       $data = curl_exec($ch);
       curl_close($ch);
       return $data;
   }


   $json = get_json($url);

   $info = json_decode($json,true);




//Prints the outer variables

   print "This is the end_time for the chart : ";
   print $info["response"]["end_time"]."<br />";

   print "This is the name for the chart : ";
   print $info["response"]["name"]."<br />";

   print "This is the UUID of the chart : ";
   print $info["response"]["id"]."<br />";


   print "This is the period for the chart : ";
   print $info["response"]["period"]."<br />";

   /* gets the json data from the URL */




?>

推荐答案

如何?

$stuff = json_decode($json, true);

$results = array();

foreach($stuff['response']['data'] as $chunk) {
  $artist = $chunk['artist'];
  $id   = $artist['id'];
  $name = $artist['name'];

  $rank = $chunk['rank'];

  $tuple = array($id, $name, $rank);

  $results[] = $tuple;
}

这篇关于解析嵌套的JSON以检索嵌套的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆