doushi2047 2014-04-01 11:48
浏览 16

JSON解析空结果

I have the following example JSON:

{
productAPI: {
  status: 200,
  message: "OK",
  version: 3,
  products: {
    grouped: {
      matches: 773949,
      groups: [
        {
          doclist: {
            start: 0,
            numFound: 385957,
            docs: [
              {
                merchant: "Amazon",
                currency: "GBP"
               }
            ]
         },
         doclist: {
            start: 0,
            numFound: 885957,
            docs: [
              {
                merchant: "Ebay",
                currency: "GBP"
               }
            ]
         }

There will be multiple Merchant names I want to print but getting blank result (I am quite new to JSON parsing)

Here's what I'm trying:

$prodList=$prodList['productAPI']['products']['grouped']['groups']['doclist']['docs'];

foreach ($prodList as $element){
    echo $element['merchant'];
}

Is this the correct method?

UPDATE

As has been correctly pointed out (I didn't add enough example) 'groups' is an array, so, I've changed to the following but still no joy:

$prodList=$prodList['productAPI']['products']['grouped']['groups'];

$i=0;
foreach ($prodList as $element){
    echo $element[$i]['doclist']['docs']['merchant'];
    $i++;
}
  • 写回答

1条回答 默认 最新

  • duanjia4817 2014-04-01 11:53
    关注

    Value under groups index is an array and you miss the index to this array. Maybe something like this would help:

    $prodList=$prodList['productAPI']['products']['grouped']['groups'][0]['doclist']['docs']; 
    

    (depending on what can be in the groups array and what you want to do about it).

    Update Now there is conversely too much indirection to the groups array :). Try this:

    $prodList=$prodList['productAPI']['products']['grouped']['groups'];
    foreach ($prodList as $element){
        $docs = $element['doclist']['docs'];
        foreach ($docs as $doc) {
            echo $doc['merchant'];
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答