无法弄清楚PHP的JSON格式

时间:2013-11-08 03:02:59

标签: php json

[{"text":"\n        $3.99\n      ","attrs":{"class":"priceLarge"}}]

这就是JSON回归给我的方式......(使用print_r($ price)显示)...

我已经尝试过各种方法在PHP中访问这些数据,但没有任何效果。

我想要“文字”......

谢谢!

编辑:

var_dump(根据要求)     string(96)“[{”text“:”\ n $ 3.99 \ n“,”attrs“:{”class“:”priceLarge“}}]”

3 个答案:

答案 0 :(得分:1)

使用 json_decode()

<?php
$jsonStr='[{"text":"\n        $3.99\n      ","attrs":{"class":"priceLarge"}}]';
$jsonArr = json_decode($jsonStr);
echo $jsonArr[0]->text;

答案 1 :(得分:1)

假设您有一个JSON字符串,您需要先使用json_decode()将其转换为PHP对象:

$json = json_decode($jsonString);

从那里,您可以从数据中的第一个对象访问text(这是一个数组,由外方括号[]定义:

echo $json[0]->text;
//         |     |
//         |     The property text of that first element.
//         |
//         The first element in the array of data.

答案 2 :(得分:1)

$string = '[{"text":"\n        $3.99\n      ","attrs":{"class":"priceLarge"}}]';

$obj = json_decode($string);

允许您访问:

print $obj[0]->text;