如何将JSON字符串转换为数组

XMe*_*Men 115 php arrays json

我想做的是以下内容:

  1. 从PHP中的文本区域输入JSON作为输入
  2. 使用此输入并将其转换为JSON并将其传递给php curl以发送请求.

这是我从PHP获取api这个json字符串我想传递给json,但它没有转换为数组

echo $str='{
        action : "create",
        record: {
            type: "n$product",
            fields: {
                n$name: "Bread",
                n$price: 2.11
            },
            namespaces: { "my.demo": "n" }
        }
    }';
    $json = json_decode($str, true);
Run Code Online (Sandbox Code Playgroud)

上面的代码没有返回我的数组.

Rik*_*kus 174

如果您在帖子中传递JSON json_decode,它将失败.有效的JSON字符串有引用键:

json_decode('{foo:"bar"}');         // this fails
json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar")
json_decode('{"foo":"bar"}');       // returns an object, not an array.
Run Code Online (Sandbox Code Playgroud)


小智 93

试试这个:

$data = json_decode($your_json_string, TRUE);
Run Code Online (Sandbox Code Playgroud)

第二个参数将解码的json字符串转换为关联数组.


小智 29

如果您使用,或者从表单获取JSON字符串$_REQUEST,则需要使用该函数.我没有意识到这一点,直到我做了请求中的内容与我复制到的内容和语句,并注意到请求字符串要大得多.$_GET$_POSThtml_entity_decode()var_dumpecho

正确的方式:

$jsonText = $_REQUEST['myJSON'];
$decodedText = html_entity_decode($jsonText);
$myArray = json_decode($decodedText, true);
Run Code Online (Sandbox Code Playgroud)

有错误:

$jsonText = $_REQUEST['myJSON'];
$myArray = json_decode($jsonText, true);
echo json_last_error(); //Returns 4 - Syntax error;
Run Code Online (Sandbox Code Playgroud)

  • 完美,这是有效的.当我从$ _POST函数获取数据时json_last_error()is = to JSON_ERROR_SYNTAX.但总的来说还不错.是ascii或utf8等编码的错误,不是编码错误.谢谢 (2认同)

小智 11

使用json_decode($json_string, TRUE)函数将JSON对象转换为数组.

例:

$json_string   = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

$my_array_data = json_decode($json_string, TRUE);
Run Code Online (Sandbox Code Playgroud)

注意:第二个参数将解码的JSON字符串转换为关联数组.

===========

输出:

var_dump($my_array_data);

array(5) {

    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您使用URL获取json字符串file_get_contents,请按照以下步骤操作:

$url = "http://localhost/rest/users";  //The url from where you are getting the contents
$response = (file_get_contents($url)); //Converting in json string
 $n = strpos($response, "[");
$response = substr_replace($response,"",0,$n+1);
$response = substr_replace($response, "" , -1,1);
print_r(json_decode($response,true));
Run Code Online (Sandbox Code Playgroud)


小智 6

您的字符串应采用以下格式:

$str = '{"action": "create","record": {"type": "n$product","fields": {"n$name": "Bread","n$price": 2.11},"namespaces": { "my.demo": "n" }}}';
$array = json_decode($str, true);

echo "<pre>";
print_r($array);
Run Code Online (Sandbox Code Playgroud)

输出:

Array
 (
    [action] => create
    [record] => Array
        (
            [type] => n$product
            [fields] => Array
                (
                    [n$name] => Bread
                    [n$price] => 2.11
                )

            [namespaces] => Array
                (
                    [my.demo] => n
                )

        )

)
Run Code Online (Sandbox Code Playgroud)