使用php解析,过滤和显示多级json

时间:2013-06-18 18:03:26

标签: php json

完全是JSON的新手,我的任务是使用php过滤/排序远程JSON并将格式化的结果嵌入到CMS中。

数据结构如下所示:

"Categories":[
  {
     "Name":"Americas",
     "ID":"12345",
     "Countries":[
        {
           "Name":"Argentina",
           "Partners":[
              {
                 "Country":"Argentina",
                 "ID":"4321",
                 "LogoUrl":"logo1.jpg",
                 "Title":"Company A",
                 "AddressBlock":"123 Main Street",
                 "Phone":"444-555-1212",
                 "TollFree":"",
                 "Email":"info@CompanyA.com",
                 "Url":"http://www.CompanyA.com/",
                 "IsVisible":true,
                 "IsDistributor":false
              }
           ]
        },
        {
           "Name":"Brazil",
           "Partners":[
              {
                 "Country":"Brazil",
                 "ID":"5432",
                 "LogoUrl":"logo2.jpg",
                 "Title":"Company B",
                 "AddressBlock":"54 Center Street",
                 "Phone":"234-567-3600",
                 "TollFree":"",
                 "Email":"info@CompanyB.com",
                 "Url":"http://www.CompanyB.com",
                 "IsVisible":true,
                 "IsDistributor":false
              },

    "Name":"Canada",
           "Partners":[
              {
                 "Country":"Canada",
                 "ID":"Company C",
                 "LogoUrl":"logo3.Company C",
                 "AddressBlock":"1 Mll Road Floor 27\r\nCanton, ON",
                 "Phone":"555-66-7777",
                 "TollFree":"",
                 "Email":"info@CompanyC.com",
                 "Url":"http://www.CompanyC.com",
                 "IsVisible":true,
                 "IsDistributor":false
              },

           ]
        }
     ]
  }

理想情况下,我想将键/值对存储在一个数组中,然后将它们打印出来作为按字母顺序排序的列表。每个国家/地区可以有多个条目,并且需要隐藏设置为“IsVisible:false”的条目。

我在这里做了一些搜索,我可以访问数据源,但数组不是 通过php'爆炸'或通过它的尺寸循环并返回:

Categories:Array 

使用此代码:

 $string = file_get_contents("https://myURL.securekey");
foreach ($json_a as $key => $value)
 {
   foreach($value as $v)
   {
       echo $v."  ";
   }
 }

任何帮助都会非常感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

编辑:所以使用以下内容:

 $string =  file_get_contents('https://secure.json');
 $json = json_decode($string, true);
 foreach($json as $fieldIndex => $fields) { 
    foreach($fields as $valueIndex => $envelope) {
        foreach($envelope as $valueEntry) {
            foreach($valueEntry as $key => $value) {
                if ($key == "Name") {
                echo $value;
                }
                printf("%d - %d - %s: '%s'\n", $key, $value);
                $build[$valueIndex][$key]=$value;               
            }
        }
    }
}
var_dump($build);

我得到以下输出:

0 - 0 - 0:'数组'0 - 0 - 1:'数组'0 - 0 - 2:'数组'0 - 0 - 3:'数组'0 - 0 - 4:'数组'0 - 0 - 5:'数组'数组(1){[0] => array(6){[0] => array(2){[“Name”] => string(9)“Argentina”[“Partners”] => array(1){[0] => array(11){[“Country”] => string(9)“Argentina”[“ID”] => string(36)“4d93”[“LogoUrl”] => string(52)“http://www.aaa.com/images/partners/aaa.jpg”[“Title”] => string(14)“S.A.公司” [ “AddressBlock”] => string(118)“Main Street - Dock 8 12435- Anytown USA”[“Phone”] => string(17)“(444)123-4567”[“TollFree”] => string(0)“”[“电子邮件”] => string(28)“info@aaa.com”[“Url”] => string(35)“http://www.aaaa.com/”[“IsVisible”] => bool(true)[“IsDistributor”] => bool(false)}}}

但仍然不确定如何引用特定的名称/值以在需要时回显它们。我假设可能是这样的:

if ($key == "Title") {
  echo "Title: " . $key . "<br />";
}  esleif ($key == "Country") {
  echo $value;
 }

但我被困在a)把它放在循环中的位置,所以它实际上抓住了正确级别的值和b)语法(必须关闭,因为我仍然只能在末尾看到任何带有var_dump的东西比回出循环中的值。