簡體   English   中英

將json字符串轉換為php關聯數組

[英]Convert json string to php associative array

不知道為什么它不會轉換,我認為它可能與字符串有關,但是我得到了np輸出。

$string = '[{title : "Comp 1 Product",columns : ["Our Vehicle","Features","Their Vehicle"], items : [["dcq","adv","asdvasdv"],["sdv","sdv","sdv"]]},{title : "qwefqw",columns : ["Section 1","Features","Section 2"],items : [["qqwec","qwe","qwegqwev"]]}]';

print_r(json_decode($string), true);

任何幫助,將不勝感激!

如果你看到:

<?php
    header("Content-type: text/plain");
    $string = '[{title : "Comp 1 Product",columns : ["Our Vehicle","Features","Their Vehicle"], items : [["dcq","adv","asdvasdv"],["sdv","sdv","sdv"]]},{title : "qwefqw",columns : ["Section 1","Features","Section 2"],items : [["qqwec","qwe","qwegqwev"]]}]';
    print_r(json_decode($string), true);
    print_r(json_last_error());
?>

上面的代碼返回4 ,表示JSON_ERROR_SYNTAX ,這是JSON的語法錯誤。 使用JSON Lint進行檢查時,您的JSON拋出:

前言

您需要更正它,使其看起來像:

[{
    "title": "Comp 1 Product",
    "columns": ["Our Vehicle", "Features", "Their Vehicle"],
    "items": [
        ["dcq", "adv", "asdvasdv"],
        ["sdv", "sdv", "sdv"]
    ]
}, {
    "title": "qwefqw",
    "columns": ["Section 1", "Features", "Section 2"],
    "items": [
        ["qqwec", "qwe", "qwegqwev"]
    ]
}]

您現在擁有的是一個JavaScript對象,而不是有效的JSON

除了無效的json外, print_r(json_decode($string), true); 不輸出任何內容,但返回值。 要將結果打印到輸出,您需要執行以下任一操作:

print_r(json_decode($string));

要么

echo print_r(json_decode($string), true);

前者更好。

暫無
暫無

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

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