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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100