为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 php 中对原始 json 字符串进行编码

如何在 php 中对原始 json 字符串进行编码

PHP
千万里不及你 2022-09-17 21:06:15
我有JSON,它已保存在我网站的文本框中,并保存到下面的数据库中。{"@type":"GOOOGLE.COM","@id":"GOOGLE","url":"GOOGLE","inLanguage":"en-US","name":"dfghjk | Bloomberg Professional Services","isPartOf":{"@id":"GOOGLE,"datePublished":"2020-02-11T21:51:45+00:00","dateModified":"2020-02-11T21:51:45+00:00"}]}我想将其转换为PHP中的正确外观,如下所示:{"@type":"GOOOGLE.COM","@id":"GOOGLE","url":"GOOGLE","inLanguage":"en-US","name":"dfghjk | Bloomberg Professional Services","isPartOf":{"@id":"GOOGLE,"datePublished":"2020-02-11T21:51:45+00:00","dateModified":"2020-02-11T21:51:45+00:00"}]}我试着做了一个json_encode但它不起作用。如何将带有所有这些转义引号等的字符串转换为PHP中的普通json字符串?我试过这样:  $str = "{"@type":"GOOOGLE.COM","@id":"GOOGLE","url":"GOOGLE","inLanguage":"en-US","name":"dfghjk | Bloomberg Professional Services","isPartOf":{"@id":"GOOGLE,"datePublished":"2020-02-11T21:51:45+00:00","dateModified":"2020-02-11T21:51:45+00:00"}]}"  $data = json_encode($str, true);    return $data; 
查看完整描述

1 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

您需要将 HTML 实体解码回字符:


https://www.php.net/manual/en/function.htmlspecialchars-decode.php


  $json = htmlspecialchars_decode($str, true);  // valid json string

  $data = json_decode($json); // convert json to php data structure

  return $data;


查看完整回答
反对 回复 2022-09-17
  • 1 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信