Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从CURL响应的JSON字符串中删除引号_Php_Json_Curl_Php Curl - Fatal编程技术网

PHP从CURL响应的JSON字符串中删除引号

PHP从CURL响应的JSON字符串中删除引号,php,json,curl,php-curl,Php,Json,Curl,Php Curl,php curl请求的响应显示为 “{\'result\':\'success\',\'entry\':\'22\',\'confirm\':\'yes\'” 但是,输出不应该在引号前面有\。 如何删除这些引号并正确返回JSON //...snip.. $result = curl_exec($ch); curl_close($ch); return $result; 我尝试的几个选项是返回打印($result)。这是预期的回归,但我认为这不是正确的方式 PHP版本-

php curl请求的响应显示为

“{\'result\':\'success\',\'entry\':\'22\',\'confirm\':\'yes\'”

但是,输出不应该在引号前面有
\
。 如何删除这些引号并正确返回JSON

 //...snip..
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
我尝试的几个选项是
返回打印($result)
。这是预期的回归,但我认为这不是正确的方式

PHP版本-5.6.16

您可以使用删除JSON字符串中的斜杠,然后使用对JSON字符串进行解码

像这样,

{
"result":"success",
"entry":"22",
"confirm":"yes"
}
$json_string="{\"result\":\"success\",\"entry\":\"22\",\"confirm\":\"yes\"}";
$json_string=stripslashes($json_string);
$json_array=json_decode($json_string,true);
print_r($json_array);

上面的方法只是从字符串中删除斜杠,并使用json\u decode()对json字符串进行解码

但您也可以直接用斜杠解码字符串。(感谢@jeroen) 像这样,

{
"result":"success",
"entry":"22",
"confirm":"yes"
}
$json_string="{\"result\":\"success\",\"entry\":\"22\",\"confirm\":\"yes\"}";
$json_string=stripslashes($json_string);
$json_array=json_decode($json_string,true);
print_r($json_array);


json_decode()中的第二个参数表示要在数组中解析json字符串,而不是默认行为的对象

您的输出是正确的,并且您拥有有效的json;这是一个字符串

你所要做的就是解码它:

$json_string="{\"result\":\"success\",\"entry\":\"22\",\"confirm\":\"yes\"}";
$json_array=json_decode($json_string,true);
print_r($json_array);

.

只需在curl中使用以下标题选项,即可返回json对象:

$s = "{\"result\":\"success\",\"entry\":\"22\",\"confirm\":\"yes\"}";

var_dump(json_decode($s));

因为默认的接受类型是Text/Plain,所以它将返回您解析的sting。通过设置上面的标题,您将收到json对象。

不需要使用
stripslashes()
,您可以直接对其进行解码。这是真的,不需要stripslashes。如果添加stripslashes,json字符串将被破坏,但
json_decode
会删除诸如result、entry、confirm之类的键。@blakcaps你的意思是什么,你检查过这个示例了吗?它返回一个带有键
result
entry
等的对象。@blakcaps yes jeroen是对的。。它返回一个仅包含键的对象。。请检查。我正在尝试从方法返回
$result
。如何将
var\u dump
用于方法return@blakcaps
var\u dump()
只是显示结果,如果需要对象(或数组…),只需返回
json\u decode()的结果即可。