簡體   English   中英

json_decode 到數組

[英]json_decode to array

我正在嘗試將 JSON 字符串解碼為數組,但出現以下錯誤。

致命錯誤:不能在第 6 行的 C:\wamp\www\temp\asklaila.php 中使用 stdClass 類型的對象作為數組

這是代碼:

<?php
$json_string = 'http://www.domain.com/jsondata.json';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj['Result']);
?>

根據文檔,如果您想要一個關聯數組而不是來自json_decode的對象,則需要指定true作為第二個參數。 這將是代碼:

$result = json_decode($jsondata, true);

如果您想要integer鍵而不是任何屬性名稱:

$result = array_values(json_decode($jsondata, true));

但是,使用您當前的解碼,您只需將其作為對象訪問:

print_r($obj->Result);

嘗試這個

$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
echo "<pre>";
print_r($obj);

這是一個較晚的貢獻,但有一個有效的案例可以使用(array)轉換json_decode
考慮以下:

$jsondata = '';
$arr = json_decode($jsondata, true);
foreach ($arr as $k=>$v){
    echo $v; // etc.
}

如果$jsondata曾經作為空字符串返回(根據我的經驗,它經常是這樣), json_decode將返回NULL ,導致錯誤Warning: Invalid argument provided for foreach() on line 3 您可以添加一行 if/then 代碼或三元運算符,但 IMO 只需將第 2 行更改為 ...

$arr = (array) json_decode($jsondata,true);

...除非您json_decode處理數百萬個大型數組,在這種情況下,正如@TCB13 指出的那樣,性能可能會受到負面影響。

根據PHP 文檔json_decode函數有一個名為assoc的參數,它將返回的對象轉換為關聯數組

 mixed json_decode ( string $json [, bool $assoc = FALSE ] )

由於assoc參數默認為FALSE ,因此您必須將此值設置為TRUE才能檢索數組。

檢查以下代碼以獲取示例含義:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));

輸出:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

這也將其更改為數組:

<?php
    print_r((array) json_decode($object));
?>

json_decode支持第二個參數,當它設置為TRUE時,它將返回一個Array而不是stdClass Object 檢查json_decode函數的手冊頁以查看所有支持的參數及其詳細信息。

例如試試這個:

$json_string = 'http://www.example.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, TRUE); // Set second argument as TRUE
print_r($obj['Result']); // Now this will works!
json_decode($data, true); // Returns data in array format 

json_decode($data); // Returns collections 

因此,如果想要一個數組,則可以在json_decode函數中將第二個參數作為“true”傳遞。

我希望這能幫到您

$json_ps = '{"courseList":[  
            {"course":"1", "course_data1":"Computer Systems(Networks)"},  
            {"course":"2", "course_data2":"Audio and Music Technology"},  
            {"course":"3", "course_data3":"MBA Digital Marketing"}  
        ]}';

使用 Json 解碼功能

$json_pss = json_decode($json_ps, true);

在php中循環JSON數組

foreach($json_pss['courseList'] as $pss_json)
{

    echo '<br>' .$course_data1 = $pss_json['course_data1']; exit; 

}

結果:計算機系統(網絡)

在 PHP json_decode 中將 json 數據轉換為 PHP 關聯數組
例如: $php-array= json_decode($json-data, true); print_r($php-array); $php-array= json_decode($json-data, true); print_r($php-array);

請試試這個

<?php
$json_string = 'http://www.domain.com/jsondata.json';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata, true);
echo "<pre>"; print_r($obj['Result']);
?>

試試這樣:

$json_string = 'https://example.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj->Result);
foreach($obj->Result as $value){
  echo $value->id; //change accordingly
}

暫無
暫無

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

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