PHP从CURL响应的JSON字符串中删除引号
php curl请求的响应显示为 “{\'result\':\'success\',\'entry\':\'22\',\'confirm\':\'yes\'” 但是,输出不应该在引号前面有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版本-
\
。
如何删除这些引号并正确返回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@blakcapsvar\u dump()
只是显示结果,如果需要对象(或数组…),只需返回json\u decode()的结果即可。