dongweicha6077 2017-04-06 08:27
浏览 58
已采纳

如何使用php读取JSON文件

I have a following JSON File. I have tried accessing it; but, I am getting undefined error. Please suggest how to read it using PHP:

"feed": {
   "entry": [
   {
    "id": "679244143963",
    "title": "Nashware Black Travel Kit",
    "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
    "link": "http://www.sl.com/product/travel-kit/679244143963",
    "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
    "sub_category_id": "435",
    "sub_category_name": "Travel Accessories",
    "mrp": "700",
    "availability": "in stock",
    "effective_price": "310"
  },
   {
    "id": "679244143963",
    "title": "Nashware Black Travel Kit",
    "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
    "link": "http://www.sl.com/product/travel-kit/679244143963",
    "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
    "sub_category_id": "435",
    "sub_category_name": "Travel Accessories",
    "mrp": "700",
    "availability": "in stock",
    "effective_price": "310"
  }
   ]
  }

After Entry same as JSON notation Id, title, desc and etc are displayed in array. Any suggestions.

  • 写回答

2条回答 默认 最新

  • doucang2831 2017-04-06 08:43
    关注

    It seems your JSON is structured as follows :

    {"feed" : {
       "entry" : [array of JSON objects]
     }
    }
    

    So after doing some research, it seems the best way to do it would be to use RecursiveArrayIterator. I found the answer on another post, here.

     <?php
    
    $json = <<< JSON
    {
     "feed": {
            "entry": [{
                "id": "679244143963",
                "title": "Nashware Black Travel Kit",
                "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
                "link": "http://www.sl.com/product/travel-kit/679244143963",
                "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
                "sub_category_id": "435",
                "sub_category_name": "Travel Accessories",
                "mrp": "700",
                "availability": "in stock",
                "effective_price": "310"
            },
            {
                "id": "679244143963",
                "title": "Nashware Black Travel Kit",
                "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
                "link": "http://www.sl.com/product/travel-kit/679244143963",
                "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
                "sub_category_id": "435",
                "sub_category_name": "Travel Accessories",
                "mrp": "700",
                "availability": "in stock",
                "effective_price": "310"
            }]
        }
    }
    JSON;
    
    $jsonIterator = new RecursiveIteratorIterator(
        new RecursiveArrayIterator(json_decode($json, TRUE)),
        RecursiveIteratorIterator::SELF_FIRST);
    
    foreach ($jsonIterator as $key => $val) {
        if(is_array($val)) {
            echo "$key:
    ";
        } else {
            echo "$key => $val
    ";
        }
    }
    

    Here you can run the code one codepad

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录