簡體   English   中英

無法弄清楚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“:”\\ n $ 3.99 \\ n“,”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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM