Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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中编码原始json字符串_Php_Json - Fatal编程技术网

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

如何在php中编码原始json字符串,php,json,Php,Json,我有一个JSON,它已经保存在我网站上的一个文本框中,并保存到下面的一个数据库中 {"@type":"GOOOGLE.COM","@id":"GOOGLE","url":"GOOGLE","inLanguage":"en-US&a

我有一个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_编码,但它不起作用。如何将包含所有转义引号等的字符串转换为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; 

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


即使在解码HTML实体之后,仍然存在一些语法错误-嵌套的
“GOOGLE
值没有正确引用,并且在末尾有一个备用的
]
。它需要手动修复。似乎不起作用,最后它显示为null,使用调试器我看到它没有改变看起来像
$json=htmlspecialchars\u decode($str,true)
应该是
$json=htmlspecialchars\u decode($str)
此外,提供的JSON字符串不是有效的JSON。有几个引号放错地方了,我有没有办法把外面的引号也去掉,换成单引号<代码>“{@type:”GOOOGLE.COM“,“@id:”GOOGLE“,”url:”GOOGLE“,”inLanguage:”en US“,”name:”dfghjk |彭博专业服务“,”iPartof:“{@id:”GOOGLE,“datePublished:”2020-02-11T21:51:45+00:00“,”dateModified:”2020-02-11T21:51:45+00“}”“JSON标准对字符串使用双引号。如果使用singe转换双引号,将进一步破坏JSON。我不认为有一个适合所有人的解决方案来修复损坏的JSON。
  $json = htmlspecialchars_decode($str, true);  // valid json string
  $data = json_decode($json); // convert json to php data structure
  return $data;