解析数组的JSON数组

时间:2016-06-08 07:01:42

标签: javascript json

我有一个数组的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… ]

如何解析此数组以获取值

1 个答案:

答案 0 :(得分:1)

json_encode()是一个从PHP数组中创建JSON字符串的函数。

  

这里要注意的重要一点是PHP array有点类似于   Javascript Object包含keyvalue对。

因此,在javascript中获取变量ar之后,您只需要JSON.parse(ar),然后通过以下任何循环(或任何其他自定义函数)遍历变量:< / p>

  • forEach
  • for in循环
  • for of循环