dongshungou7699 2015-12-08 15:58
浏览 23
已采纳

我怎么能通过PHP解析这个json文件?

Im trying to parse this json file that has my store's products using php but with no luck! I'm new to php and I tried searching online but nothing had the answer. Here is part of my json file:

    [
  {
    "_type": "default",
    "description": [
      "Product Description comes here"
    ],
    "url": "here is the url to my seo friendly url",
    "price": [
      "11.64"
    ],
    "sizes": [
      "sizes come here"
    ],
    "images": [
      "image name and address is here"
    ],
    "_template": "5c1333451e128f546e451197f6c6061b3a66d1ee",
    "_cached_page_id": "5119b553b1512ae1a19d6cd0d3f29e51e111a90c",
    "name": [
      "product name is here"
    ]
  },
  {
    "_type": "default",
    "description": [
      "Product #2 Description comes here"
    ],
    "url": "here is the url to my seo friendly url",
    "price": [
      "11.64"
    ],
    "sizes": [
      "sizes come here"
    ],
    "images": [
      "image name and address is here"
    ],
    "_template": "5c1333451e128f546e451197f6c6061b3a66d1ee",
    "_cached_page_id": "5119b553b1512ae1a19d6cd0d3f29e51e111a90c",
    "name": [
      "product #2 name is here"
    ]
  }
]

This is just a sample of the whole json file but it follows this format. I tried writing the following php code to index it but as I said, no luck:

    <?php
// this code reads from the files provided to it and populates the     database with products

$jsondata = file_get_contents("55products.json");
$json = json_decode($jsondata, true);


foreach ($json["products"] as $product)
{
    // now lets split it into smaller pieces
    foreach ($product as $product_infos) {
        print_r($product_infos);
        echo "<br /><br /><br />";


    }

}

But still can't access the contents or to be specific, I don't know how to access the data store in there. I understand this might be a very stupid question but all the tutorials online don't have a similar syntax so please help me out. Best regards.

  • 写回答

1条回答 默认 最新

  • dtz88967 2015-12-08 16:05
    关注

    The inner elements are arrays themselves, you'll need to access the values using the index 0

    $obj = json_decode($json,true);
    // var_dump($obj);exit;
    
    foreach ($obj as $product)
    {
        print $product["description"][0] . "<br/>";
        print $product["images"][0] . "<br/>";
        print $product["url"] . "<br/>";
        print $product["name"][0] . "<br/>";
    }
    

    Will ouput

    Product Description comes here
    image name and address is here
    here is the url to my seo friendly url
    product name is here
    Product #2 Description comes here
    image name and address is here
    here is the url to my seo friendly url
    product #2 name is here
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网