weeo weeo - 1 year ago 34
JSON Question

Refer to a variable start with @ from json in php

Hi I know that I can refer to json variable after decoding in php using this following way:

$data = json_decode(file_get_contents("php://input"));
$myfile = fopen("aaaa.txt", "w") or die("Unable to open file!");
fwrite($myfile, $data->form->variable);
fclose($myfile);


However, there is a variable starts with @ in json.
how do I refer to that variable?
$data->form->@name
doesn't work and
$data->form['@name']
doesn't work either.

here is my json file: (I want the @name)

{
"build_id": "d776ccf19c994f65b8605ecc0711d94b",
"archived": false,
"uiversion": "1",
"attachments": {},
"form": {
"@uiVersion": "1",
"@xmlns": "http://openrosa.org/formdesigner/77618A3E-48A4-474D-9AC5-80D7BFA952F7",
"@name": "External Good Receiving Note",
"Truck_num": "f",
"invoice_num": "fd",
"#type": "data",
"driver_signature": "",
"arrival_time": "2017-05-22T19:31:00.000000Z",
"driver_name": "f",
"meta": {
"@xmlns": "http://openrosa.org/jr/xforms",
"app_build_version": 118,
"commcare_version": "2.35.3",
"instanceID": "8e895f2c-30c6-4ba9-979c-e99af9f4ad1c",
"username": "wang",
"userID": "306dd277c96773330613bfa78005a596",
"appVersion": "CommCare Android, version \"2.35.3\"(431724). App v118. CommCare Version 2.35. Build 431724, built on: 2017-04-19",
"timeEnd": "2017-05-22T19:31:48.042000Z",
"timeStart": "2017-05-22T19:31:26.109000Z",
"geo_point": null,
"deviceID": "352331080741542"
},
"officer": "ff",
"Signature": "",
"waybill_num": "f",
"warehouse_code": "ff",
"@version": "118",
"PO": "15",
"comment": "ff",
"name": {
"@xmlns": "grn"
}
},
"initial_processing_complete": true,
"received_on": "2017-05-22T19:31:48.170284Z",
"app_id": "0d24ecddaf46a8516780a7ab32b1d120",
"is_phone_submission": "2.0",
"version": "118",
"domain": "crs-emergency-response-system",
"problem": null,
"resource_uri": "",
"type": "data",
"id": "8e895f2c-30c6-4ba9-979c-e99af9f4ad1c",
"metadata": {
"app_build_version": 118,
"doc_type": "Metadata",
"commcare_version": "2.35.3",
"instanceID": "8e895f2c-30c6-4ba9-979c-e99af9f4ad1c",
"deprecatedID": null,
"username": "wang",
"userID": "306dd277c96773330613bfa78005a596",
"appVersion": "CommCare Android, version \"2.35.3\"(431724). App v118. CommCare Version 2.35. Build 431724, built on: 2017-04-19",
"timeEnd": "2017-05-22T19:31:48.042000",
"timeStart": "2017-05-22T19:31:26.109000",
"geo_point": null,
"deviceID": "352331080741542",
"location": null
}
}


The file after json_decode, it still has @ symbol before those variables:

stdClass Object
(
[app_id] => 0d24ecddaf46a8516780a7ab32b1d120
[archived] =>
[attachments] => stdClass Object
(
)

[build_id] => d776ccf19c994f65b8605ecc0711d94b
[domain] => crs-emergency-response-system
[form] => stdClass Object
(
[#type] => data
[@name] => External Good Receiving Note
[@uiVersion] => 1
[@version] => 118
[@xmlns] => http://openrosa.org/formdesigner/77618A3E-48A4-474D-9AC5-80D7BFA952F7
[PO] => 15
[Signature] =>
[Truck_num] => f
[arrival_time] => 2017-05-22T19:31:00.000000Z
[comment] => ff
[driver_name] => f
[driver_signature] =>
[invoice_num] => fd
[meta] => stdClass Object
(
[@xmlns] => http://openrosa.org/jr/xforms
[appVersion] => CommCare Android, version "2.35.3"(431724). App v118. CommCare Version 2.35. Build 431724, built on: 2017-04-19
[app_build_version] => 118
[commcare_version] => 2.35.3
[deviceID] => 352331080741542
[geo_point] =>
[instanceID] => 8e895f2c-30c6-4ba9-979c-e99af9f4ad1c
[timeEnd] => 2017-05-22T19:31:48.042000Z
[timeStart] => 2017-05-22T19:31:26.109000Z
[userID] => 306dd277c96773330613bfa78005a596
[username] => wang
)

[name] => stdClass Object
(
[@xmlns] => grn
)

[officer] => ff
[warehouse_code] => ff
[waybill_num] => f
)

[id] => 8e895f2c-30c6-4ba9-979c-e99af9f4ad1c
[initial_processing_complete] => 1
[is_phone_submission] => 2.0
[metadata] => stdClass Object
(
[appVersion] => CommCare Android, version "2.35.3"(431724). App v118. CommCare Version 2.35. Build 431724, built on: 2017-04-19
[app_build_version] => 118
[commcare_version] => 2.35.3
[deprecatedID] =>
[deviceID] => 352331080741542
[doc_type] => Metadata
[geo_point] =>
[instanceID] => 8e895f2c-30c6-4ba9-979c-e99af9f4ad1c
[location] =>
[timeEnd] => 2017-05-22T19:31:48.042000
[timeStart] => 2017-05-22T19:31:26.109000
[userID] => 306dd277c96773330613bfa78005a596
[username] => wang
)

[problem] =>
[received_on] => 2017-05-22T19:31:48.170284Z
[resource_uri] =>
[type] => data
[uiversion] => 1
[version] => 118
)

Answer Source

There you go:

$a = ['@name' => 'test'];
$json = json_encode($a);
$o = json_decode($json);

echo $o->{'@name'}; // outputs "test"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download