簡體   English   中英

如何使用php從json對象讀取所有值

[英]how to read all values from json object using php

這是我的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"
              }
            ]
          }
        }
      ]
    }

使用json_decode 它從格式正確的JSON字符串返回PHP對象

假設問題中的字符串存儲在名為$json_str的變量下,則解碼應如下所示

$json_obj = json_decode($json_str);

然后您可以像這樣訪問它

$json_obj->resourceType; // "Patient"

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

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

嘗試如下

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM