如何使用php从json对象读取所有值

时间:2016-03-29 12:59:47

标签: php arrays json

这是我的json数据:

      {
      "resourceType": "Patient",
      "birthDate": "1985-08-01T00:00:00Z",
      "active": true,
      "gender": "male",
      "deceasedBoolean": false,
      "id": "Tbt3KuCY0B5PSrJvCu2j-PlK.aiHsu2xUjUM8bWpetXoB",
      "careProvider": [
        {
          "display": "Physician Family Medicine",
          "reference": "https://open-ic.epic.com/FHIR/api/FHIR/DSTU2/Practitioner/T3Mz3KLBDVXXgaRoee3EKAAB"
        }
      ],
      "name": [
        {
          "use": "usual",
          "family": [
            "Argonaut"
          ],
          "given": [
            "Jason"
          ]
        }
      ],
      "identifier": [
        {
          "use": "usual",
          "system": "urn:oid:1.2.840.114350.1.13.327.1.7.5.737384.0",
          "value": "E3826"
        },
        {
          "use": "usual",
          "system": "urn:oid:1.2.3.4",
          "value": "203579"
        }
      ],
      "address": [
        {
          "use": "home",
          "line": [
            "1979 Milky Way Dr."
          ],
          "city": "Verona",
          "state": "WI",
          "postalCode": "53593",
          "country": "US"
        }
      ],
      "telecom": [
        {
          "system": "phone",
          "value": "608-271-9000",
          "use": "home"
        },
        {
          "system": "phone",
          "value": "608-771-9000",
          "use": "work"
        },
        {
          "system": "email",
          "value": "open@epic.com"
        }
      ],
      "maritalStatus": {
        "text": "Married"
      },
      "communication": [
        {
          "preferred": true,
          "language": {
            "text": "English",
            "coding": [
              {
                "system": "urn:oid:2.16.840.1.113883.6.99",
                "code": "en",
                "display": "English"
              }
            ]
          }
        }
      ],
      "extension": [
        {
          "url": "http://hl7.org/fhir/StructureDefinition/us-core-race",
          "valueCodeableConcept": {
            "text": "Asian",
            "coding": [
              {
                "system": "2.16.840.1.113883.5.104",
                "code": "2028-9",
                "display": "Asian"
              }
            ]
          }
        },
        {
          "url": "http://hl7.org/fhir/StructureDefinition/us-core-ethnicity",
          "valueCodeableConcept": {
            "text": "Not Hispanic or Latino",
            "coding": [
              {
                "system": "2.16.840.1.113883.5.50",
                "code": "2186-5",
                "display": "Not Hispanic or Latino"
              }
            ]
          }
        }
      ]
    }

4 个答案:

答案 0 :(得分:2)

使用json_decode。它从格式良好的JSON字符串

返回一个PHP对象

假设你问题中的字符串存储在名为$json_str的变量下,解码应该是这样的

$json_obj = json_decode($json_str);

然后您可以像这样访问它

$json_obj->resourceType; // "Patient"

答案 1 :(得分:2)

  

json_decode() - 解码JSON字符串

示例:

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

输出:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

答案 2 :(得分:1)

答案 3 :(得分:0)

尝试以下

$obj= json_decode($json,true);
if(!empty($obj->extension))
{
    foreach($obj->extension as $extension)
    {
         echo  $extension->url;
       // similarly others
     }
}