带有反斜杠的JSON(PHP)

时间:2020-09-05 14:00:22

标签: php json

我有一个问题。我想在消息中获取状态响应代码级别的值,但我无法执行它采用这种格式,有什么建议吗?

{"data":{"signUpUser":null},"errors":[{"message":"[{\"registration\":{\"status\":\"failed\",\"response_code\":\"0\",\"level\":\"U\"}}]","locations":[{"line":2,"column":3}],"path":["signUpUser"]}]}

格式化:

{
   "data":{
      "signUpUser":null
   },
   "errors":[
      {
         "message":"[{\"registration\":{\"status\":\"failed\",\"response_code\":\"0\",\"level\":\"U\"}}]",
         "locations":[
            {
               "line":2,
               "column":3
            }
         ],
         "path":[
            "signUpUser"
         ]
      }
   ]
}

3 个答案:

答案 0 :(得分:0)

“消息”是json对象的字符串化版本。您需要json_decode(message)才能检查它。

答案 1 :(得分:0)

您需要使用json_decode

$res = json_decode($json);
print_r($res);

答案 2 :(得分:0)

您需要两次json_encode,第一次是完整数据,第二次是消息字符串:

<?php
$json_string = '{"data":{"signUpUser":null},"errors":[{"message":"[{\"registration\":{\"status\":\"failed\",\"response_code\":\"0\",\"level\":\"U\"}}]","locations":[{"line":2,"column":3}],"path":["signUpUser"]}]}';

$json = json_decode($json_string);

$err_message = $json->errors[0]->message;

$err_json = json_decode($err_message);

print_r($err_json);

Here you can try the code