无法弄清楚 PHP 的 JSON 格式


Can't figure out JSON formatting for PHP

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

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

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

我想要"文本"...

谢谢!

编辑:

var_dump(根据要求) string(96) "[{"text":"' $3.99' ","attrs":{"class":"priceLarge"}}]"

利用json_decode()

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

假设你有一个 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.
$string = '[{"text":"'n        $3.99'n      ","attrs":{"class":"priceLarge"}}]';
$obj = json_decode($string);

允许您访问:

print $obj[0]->text;