我試圖使用json_decode
但我認爲[]在結果的任何一端都與它有關。解析PHP中的JSON字符串以顯示其內容
我需要訪問此JSON的某些部分。
在PHP中我做的:
<?php
$arr = json_decode($json, true);
print $arr['ua']['rawUa'];
?>
實際的JSON(從這裏開始)爲:
[
{
"meta": {
"name": "ua-parser",
"repo": "https://github.com/tobie/ua-parser",
"version": "0.3.5"
},
"ua": {
"rawUa": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2718.0 Safari/537.36",
"string": {
"family": "Chrome",
"major": "52",
"minor": "0",
"patch": "2718"
},
"family": "Chrome",
"major": 52,
"minor": 0,
"patch": 2718,
"device": {
"family": "Other"
}
},
"os": {
"string": {
"family": "Mac OS X",
"major": "10",
"minor": "11",
"patch": "1",
"patchMinor": null
},
"family": "Mac OS X",
"major": "10",
"minor": "11",
"patch": "1"
}
},
{
"meta": {
"name": "ua-parser-js",
"repo": "https://github.com/faisalman/ua-parser-js",
"version": ""
}
},
{
"meta": {
"name": "platform.js",
"repo": "https://github.com/bestiejs/platform.js/",
"version": "1.3.0"
},
"ua": {
"name": "Chrome",
"version": "52.0.2718.0",
"layout": "Blink"
},
"os": {
"os": {
"architecture": 32,
"family": "OS X",
"version": "10.11.1"
}
},
"device": {
"product": null,
"manufacturer": null,
"description": "Chrome 52.0.2718.0 on OS X 10.11.1"
}
}
]
由於包封的'[...]'的結果是一個數組,只有1元件是它雖然。因此,要訪問第一個元素,請使用'$ arr [0] ['ua'] ['key']'。使用'print_r'輸出變量有助於在代碼中找到錯誤... – giorgio