duandai0373 2016-09-12 07:33
浏览 65
已采纳

使用php从嵌套的Json中获取结果

I want to retrieve a value out of a nested JSON file with PHP. I created the following code:

<?php
$string = file_get_contents("https://api.abc");
$json = json_decode($string,true);
foreach ($json['contacts'][0]['properties']['company'] as $person) {

  echo $person['value'];
}
?>

It will only give me 1 result and only the first letter of the value. How do I get more results and more than just the first letter?

{
    "contacts": [{
        "addedAt": 1414182103652,
        "vid": 2,
        "canonical-vid": 2,
        "merged-vids": [],
        "portal-id": 448845,
        "is-contact": true,
        "properties": {
            "firstname": {
                "value": "Andre"
            },
            "lastmodifieddate": {
                "value": "1472024018755"
            },
            "company": {
                "value": "test"
            },
            "lastname": {
                "value": "Jansen"
            }
        },
  • 写回答

1条回答 默认 最新

  • doutinghou6980 2016-09-12 07:44
    关注

    try this, use only $person on the place of $person['value']

    $json = json_decode($string,true);
    foreach ($json['contacts'][0]['properties']['company'] as $person) {
      echo $person;
    }
    

    DEMO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?