douyinghuo8874 2017-09-02 13:57
浏览 783
已采纳

PHP将字符串转换为对象

I have a file that contains the following HL7 Information :

{
  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'
    }}}

I want to convert this message to a json object and I did the following :

// 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;
        }

But I get the following error :

Severity: Notice

Message: Trying to get property of non-object

When I user the getype function of PHP on the $json_file , it is a string file. How can I convert the message to an object for my own system consumption ?

  • 写回答

1条回答 默认 最新

  • doulin6448 2017-09-02 14:03
    关注

    Please validate your JSON code.

    JSON rules

    1. Data is in name/value pairs
    2. Data is separated by commas
    3. Curly braces hold objects - Your file contains no parent object
    4. Square brackets hold arrays
    5. A name/value pair consists of a field name (in double quotes). - Your name fields are not in double quotes

    Valid JSON code:

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

    Working PHP example with valid JSON code:

    <?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;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用matlab计算自定义特殊函数的二重积分,改变积分顺序所得的结果不同的问题?
  • ¥15 mysql做碎片化处理老是报错怎么办
  • ¥15 如何正确在vs2010中初始化map对象
  • ¥30 mmdet3d模型部署问题
  • ¥15 comsol仿真反射率、吸收率时峰值位置和深度不对!
  • ¥30 Visual Studio找不到sdk,如何解决?
  • ¥15 labview与机器之间网口通信发送指令
  • ¥15 迪杰斯特拉算法,考虑时延,带宽
  • ¥30 C#提交json字符串给http服务器
  • ¥15 CondaValueError: Malformed version string '~': invalid character(s).