如何在php中解析JSON字符串 [英] How to parse JSON string in php
本文介绍了如何在php中解析JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了以下JSON响应:
I'm getting below JSON response:
[{"startDate":"2012-07-12 11:21:38 +0530","totalTime":0},{"startDate":"2012-07-11 11:27:33 +0530","totalTime":0},{"startDate":"2012-07-16 18:38:37 +0530","totalTime":0},{"startDate":"2012-07-17 14:18:32 +0530","totalTime":0}]
我想要开始日期和totalTime的数组,我已经使用了这两行,但是它不能工作$ obj,请提出建议.
i want make array of start date and totalTime, i have used these two lines but it wont work $obj, please suggest..
$obj = json_decode($dateTimeArr);
$dateAr = $obj->{'startDate'};
推荐答案
正如大家所说,您做到了-使用json_decode
.
As everyone said, and you did - use json_decode
.
$dateArrays = json_decode($dateTimeArr, true); // decode JSON to associative array
foreach($dateArrays as $dateArr){
echo $dateArr['startDate'];
echo $dateArr['totalTime'];
}
将来,如果不确定变量中包含什么类型的数据或结构,请执行var_dump($var)
,它将打印变量的类型及其内容.
In future, if you are unsure what type or structure of data is in the variable, do var_dump($var)
and it will print type of variable and its content.
这篇关于如何在php中解析JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文