如何将字符串响应转换为JSON对象?

I'm calling a third party api using curl from a php function. I'm getting a response in JSON format (datatype is string). I want convert that response to an object or array. I have tried json_decode(), but I'm getting null. If I display response in browser and copy paste that string response in PHP variable, I get the value. So I can't figure out what the problem is.

Here is my code:

$fullUrl = 'http://example.com/api/assignment/1';
$data = AesCtr::encrypt($data, 'My Key', 256);
$curl = curl_init($fullUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, ['data' => $data]);
$curl_response = curl_exec($curl);
$curl_response = json_decode($curl_response);
echo '<pre>';
print_r($curl_response);

Here is response:

{"identifier":"id", "items":[{"apiResult":"INVALID", "apiResultMessage":"Invalid controls. the field 'resource' is mandatory the field 'type of item' is mandatory the field 'element id' is mandatory the field 'resource' is mandatory", "id":"", "idProject":"", "nameProject":"", "refType":"", "refId":"", "idResource":"", "nameResource":"", "idRole":"", "nameRole":"", "comment":"", "assignedWork":"", "realWork":"", "leftWork":"", "plannedWork":"", "rate":"", "realStartDate":"", "realEndDate":"", "plannedStartDate":"", "plannedEndDate":"", "dailyCost":"", "newDailyCost":"", "assignedCost":"", "realCost":"", "leftCost":"", "plannedCost":"", "idle":"", "billedWork":""}] }

I also have tried this

$curl_response = str_replace("'", "\'", $curl_response);
$curl_response = json_decode($curl_response);
doulan8054
doulan8054 查看更新的问题。
大约 4 年之前 回复
dongyan7988
dongyan7988 我有意想不到的性格。
大约 4 年之前 回复
doujiunai2169
doujiunai2169 使用json_last_error_msg()检查错误消息
大约 4 年之前 回复
dsyua2828
dsyua2828 toolong
大约 4 年之前 回复
drazvzi741287
drazvzi741287 我知道JSON是有效的。但我的问题是那为什么我不能解码呢?
大约 4 年之前 回复
duanhua9398
duanhua9398 toolong
大约 4 年之前 回复
dongnaigu2052
dongnaigu2052 toolong
大约 4 年之前 回复
dongmu5815
dongmu5815 JSON字符串似乎有效,json-parser.com/a7826645尝试使用json_last_error_msg()检查JSON解码错误消息
大约 4 年之前 回复

2个回答

Try this:-

$curl_response = curl_exec($curl);

function escapeJsonString($value) { 
    $escapers = array("\'");
    $replacements = array("\\/");
    $result = str_replace($escapers, $replacements, $value);
    return $result;
}



$curl_response = escapeJsonString($curl_response);

$curl_response = json_decode($curl_response,true);

echo '<pre>';print_r($curl_response);

echo $error = json_last_error();

Reference taken:- http://www.pontikis.net/tip/?id=5

The link you found useful is:- https://stackoverflow.com/a/20845642/4248328

dongzhe3171
dongzhe3171 问题解决了。 我从这里得到了解决方案。 请参阅评论最常见的部分。
大约 4 年之前 回复



尝试添加第二个参数“assoc”以将数组转换为json_decode函数:</ p>

$ curl_response = json_decode($ curl_response,true); </ code> </ p>

希望它能提供帮助。</ p>
</ div>

展开原文

原文

Try add second param "assoc" for getting array to json_decode function:

$curl_response = json_decode($curl_response, true);

Hope it'll help.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐