如何使用php读取JSON文件

时间:2017-04-06 08:27:25

标签: php json

我有一个以下的JSON文件。我试过访问它;但是,我得到了未定义的错误。请建议如何使用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"
  }
   ]
  }

与JSON表示法ID相同的Entry后,title,desc等显示在数组中。任何建议。

2 个答案:

答案 0 :(得分:0)

您错过了开始和结束括号。你的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"
        }]
    }
}

或者您必须使用以下代码:

$obj = json_decode("{" . $json . "}");

答案 1 :(得分:0)

看来您的JSON结构如下:

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

因此,经过一些研究,似乎最好的方法是使用RecursiveArrayIterator。我在另一篇文章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:\n";
    } else {
        echo "$key => $val\n";
    }
}

Here you can run the code one codepad