我有一个包含以下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函数时,它是一个字符串文件。 如何将消息转换为对象以供我自己的系统使用?
答案 0 :(得分:1)
JSON规则
有效的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;
?>