PHP将字符串转换为对象

时间:2017-09-02 13:57:33

标签: php json

我有一个包含以下HL7信息的文件:

{
  MESSAGE_HEADER: {
    SENDING_APPLICATION: 'IQCARE',
    SENDING_FACILITY: '10829',
    RECEIVING_APPLICATION: 'IL',
    RECEIVING_FACILITY: '10829',
    MESSAGE_DATETIME: '20170713110000',
    SECURITY: '',
    MESSAGE_TYPE: 'ADT^A04',
    PROCESSING_ID: 'P'
  },
  PATIENT_IDENTIFICATION: {
    EXTERNAL_PATIENT_ID: {
      ID: '110ec58a-a0f2-4ac4-8393-c866d813b8d1',
      IDENTIFIER_TYPE: 'GODS_NUMBER',
      ASSIGNING_AUTHORITY: 'MPI'
    }}}

我想将此消息转换为json对象,并执行以下操作:

// copy file content into a string var
        $json_file = file_get_contents("" . getcwd() . "\integration_layer\ADT^A04 - Patient Registration.json");
        echo gettype($json_file);
// convert the string to a json object
        $jfo = json_decode($json_file);
// read the title value
        $title = $jfo->MESSAGE_HEADER->SENDING_APPLICATION;
// copy the posts array to a php var
        $posts = $jfo->PATIENT_IDENTIFICATION->EXTERNAL_PATIENT_ID;
// listing posts
        foreach ($posts as $post) {
            echo $post->ID;
        }

但是我收到以下错误:

Severity: Notice

Message: Trying to get property of non-object

当我在$ json_file上使用PHP的getype函数时,它是一个字符串文件。 如何将消息转换为对象以供我自己的系统使用?

1 个答案:

答案 0 :(得分:1)

validate your JSON代码。

JSON规则

  1. 数据名称/值对
  2. 数据以逗号分隔
  3. 大括号持有对象 - 您的文件不包含父对象
  4. 方括号包含数组
  5. 名称/值对由字段名称(双引号)组成。 - 您的姓名字段不是双引号
  6. 有效的JSON代码:

        {
            "MESSAGE_HEADER": {
                "SENDING_APPLICATION": "IQCARE",
                "SENDING_FACILITY": 10829,
                "RECEIVING_APPLICATION": "IL",
                "RECEIVING_FACILITY": 10829,
                "MESSAGE_DATETIME": "20170713110000",
                "SECURITY": "",
                "MESSAGE_TYPE": "ADT^A04",
                "PROCESSING_ID": "P"
            },
            "PATIENT_IDENTIFICATION": {
                "EXTERNAL_PATIENT_ID": {
                    "ID": "110ec58a-a0f2-4ac4-8393-c866d813b8d1",
                    "IDENTIFIER_TYPE": "GODS_NUMBER",
                    "ASSIGNING_AUTHORITY": "MPI"
                }
            }   
        }
    

    使用有效的JSON代码运行PHP示例:

    <?php
    
        $json = '
            {
                "MESSAGE_HEADER": {
                    "SENDING_APPLICATION": "IQCARE",
                    "SENDING_FACILITY": 10829,
                    "RECEIVING_APPLICATION": "IL",
                    "RECEIVING_FACILITY": 10829,
                    "MESSAGE_DATETIME": "20170713110000",
                    "SECURITY": "",
                    "MESSAGE_TYPE": "ADT^A04",
                    "PROCESSING_ID": "P"
                },
                "PATIENT_IDENTIFICATION": {
                    "EXTERNAL_PATIENT_ID": {
                        "ID": "110ec58a-a0f2-4ac4-8393-c866d813b8d1",
                        "IDENTIFIER_TYPE": "GODS_NUMBER",
                        "ASSIGNING_AUTHORITY": "MPI"
                    }
                }   
            }
        ';
    
        $object = json_decode($json);
    
        echo $object->MESSAGE_HEADER->SENDING_APPLICATION;
    
    ?>