从json字符串中获取值

时间:2013-10-23 21:52:56

标签: php json

我从JSON中提取值,但在回显值

时继续获得空结果
$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]';

$obj2 = json_decode($json, true);

$displayName = $obj2->Surname;

echo"$displayName";

4 个答案:

答案 0 :(得分:1)

你在这个json字符串

中的另一个对象中有一个对象
$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]';

$obj2 = json_decode($json);

print_r($obj2);

答案 1 :(得分:0)

试试这个:

<?php
$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]';
$obj2 = json_decode($json, true);
$displayName = $obj2[0][0]['Surname'];
echo "$displayName";
?>

答案 2 :(得分:0)

这应该是这样的:

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address    Line 1":"RG412GX"}]]';

$obj2 = json_decode($json, true);

$displayName = $obj2->Surname;

echo"$displayName";

你正在混淆/组成变量名......

答案 3 :(得分:0)

您提供的代码存在一些问题。 首先,您的json文本存储在$json中,但您尝试解码$xmlresponse。我想这只是一个复制/粘贴错误。 其次,您尝试使用对象语法访问姓氏,尽管您明确强制json_decode将对象解码为关联数组。 第三,json提供了在数组中的数组中编码对象。您忽略响应的嵌套结构。

试试这个:

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]';
$response = json_decode($json);
$displayName = $response[0][0]->Surname;

echo $displayName;