简体   繁体   中英

how to read all values from json object using php

Here is my json data:

{
      "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"
              }
            ]
          }
        }
      ]
    }

use json_decode . It returns a PHP object from a well-formed JSON string

Assuming the string in your question is stored under a variable called $json_str , the decoding should go something like this

$json_obj = json_decode($json_str);

You may then access it like so

$json_obj->resourceType; // "Patient"

json_decode() - Decodes a JSON string

Example:

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

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

?>

Output:

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)
}

您可以使用PHP JSON库: http : //php.net/manual/zh/function.json-decode.php

Try as below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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