简体   繁体   English

解析JSON响应PHP

[英]Parse JSON Response PHP

I have the following JSON data which I'm trying to parse the categories: 我尝试解析以下JSON数据:

{
"skimlinksProductAPI": {
    "status": 200,
    "message": "OK",
    "version": 3,
    "categories": {
        "1": "Animals",
        "2": "Animals > Live Animals",
        "3": "Animals > Pet Supplies",
        "4": "Animals > Pet Supplies > Bird Supplies",
        "5": "Animals > Pet Supplies > Bird Supplies > Bird Cages & Stands",
        "6": "Animals > Pet Supplies > Bird Supplies > Bird Food",
        "7": "Animals > Pet Supplies > Bird Supplies > Bird Ladders & Perches",
        "8": "Animals > Pet Supplies > Bird Supplies > Bird Toys",
        ...
    }
  }
}

I'm trying the following but not working: 我正在尝试以下操作,但不起作用:

$catList = json_decode(file_get_contents('http://api-product.skimlinks.com/categories?key=MY_KEY&format=json'),true);

$catList=$catList['skimlinksProductAPI']['categories'];

foreach ($catList as $element){

    echo $element[0].' - '.$element[1];

}

The elements of categories aren't sub-arrays, they're just key-value pairs. categories的元素不是子数组,它们只是键值对。 The foreach should be: foreach应该是:

foreach ($catList as $id => $element){
     echo $id . ' - ' . $element;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM