You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中去除JSON响应字符串中的双引号?

问题:获取BlockChain API返回的钱包地址时被双引号包裹,如何去除?

我在使用BlockChain API获取钱包地址时,遇到了一个小问题:返回的地址被双引号包裹着,比如"1Dgyv8Qz1nkrJddoyLBepv1RUXSDqCBCdp"。相关代码如下:

$resp = file_get_contents($blockchain_receive_root . "v2/receive?key=" . $my_api_key . "&gap_limit=" . $gap_limit . "&callback=" . urlencode($callback_url) . "&xpub=" . $my_xpub);
$response = json_decode($resp);
print json_encode($response->address);

请问该怎么去掉这些引号?


解决方案

嘿,问题其实出在你最后一步的json_encode上!让我给你拆解清楚:

  • 首先,json_decode($resp)已经把API返回的JSON字符串转换成了PHP对象,$response->address本身就是一个不带引号的纯字符串了。
  • 但你又用json_encode()把这个字符串重新转成了JSON格式,这就硬生生给它加上了双引号。

所以最简单的解决办法就是直接输出$response->address,完全不需要再用json_encode处理:

$resp = file_get_contents($blockchain_receive_root . "v2/receive?key=" . $my_api_key . "&gap_limit=" . $gap_limit . "&callback=" . urlencode($callback_url) . "&xpub=" . $my_xpub);
$response = json_decode($resp);
// 直接打印地址即可,无需额外编码
print $response->address;

如果你担心API返回的地址可能有特殊字符(虽然区块链地址本身不会有这类问题),也可以用htmlspecialchars()做一下转义,确保输出在网页环境下的安全性:

print htmlspecialchars($response->address);

另外,给你个小建议:处理API请求时最好加上错误处理,避免空数据或者请求失败导致的报错,比如:

$resp = file_get_contents($blockchain_receive_root . "v2/receive?key=" . $my_api_key . "&gap_limit=" . $gap_limit . "&callback=" . urlencode($callback_url) . "&xpub=" . $my_xpub);
if ($resp === false) {
    die("Failed to fetch data from Blockchain API");
}
$response = json_decode($resp);
if (isset($response->address)) {
    print $response->address;
} else {
    die("Address not found in API response");
}

内容的提问来源于stack exchange,提问作者Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:25:29