我有一个数组的Php数组,json编码为
[{"TASK_ID":"79","USER_ID":"cool12",
"LATITUDE":"32.702550412612524",
"LONGITUDE":"74.85893413424492",
"STARTED_ON":"2016-06-07 14:57:05",
"STOPED_ON":"2016-06-07 14:57:05",
"DEVICE_ID":null,"DEVICE_NAME":null,
"DEVICE_TYPE":null,"PATH_ENDED":"0"},
{"TASK_ID":"80",
"USER_ID":"cool12",
"LATITUDE":"32.70255831226375",
"LONGITUDE":"74.85894754529",
"STARTED_ON":"2016-06-07 14:57:06",
"STOPED_ON":"2016-06-07 14:57:06",
"DEVICE_ID":null,
"DEVICE_NAME":null,
"DEVICE_TYPE":null,
"PATH_ENDED”:”0”}]
var ar = <?php echo json_encode($resultArray) ?>;
给出了这个结果
Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, 25 more… ]
如何解析此数组以获取值
答案 0 :(得分:1)
json_encode()
是一个从PHP数组中创建JSON
字符串的函数。
这里要注意的重要一点是
PHP array
有点类似于Javascript Object
包含key
和value
对。
因此,在javascript中获取变量ar
之后,您只需要JSON.parse(ar)
,然后通过以下任何循环(或任何其他自定义函数)遍历变量:< / p>
forEach
for in
循环for of
循环