是否有任何在线工具可用于将无效 JSON 中的单引号替换为双引号或在 PHP 中使用无效 json 的任何解决方案?
Posted
技术标签:
【中文标题】是否有任何在线工具可用于将无效 JSON 中的单引号替换为双引号或在 PHP 中使用无效 json 的任何解决方案?【英文标题】:Are there any online tools for replacing single quote to double quote in invalid JSON or any solution to use the invalid json in PHP? 【发布时间】:2019-10-05 16:09:51 【问题描述】:我有很多无效的 JSON 文件。
这是无效 JSON 文件的 sn-p。
[
'name':'Arden',
'born':'1973-12-22',
'orientation':'Heterosexual',
'purpose':'Friendship',
'tags':'cooking,swimming,adventure,drawing',
'country':'United Kingdom::Edinburgh',
'about':'I often read (and enjoy!) things that are completely opposed to everything I believe. I've enjoyed.'
,
'name':'Sisera',
'born':'1962-01-25',
'orientation':'Homosexual',
'purpose':'Flirt',
'tags':'reading,fishing,tennis,theater',
'country':'United States::Fairfield',
'about':'I'm OCD about shoes being placed side-by-side. I don't like them together by the door.'
]
如何将此单引号替换为双引号? php中有没有在线工具或解决方案?
我想要的是:
[
"name":"Arden",
"born":"1973-12-22",
"orientation":"Heterosexual",
"purpose":"Friendship",
"tags":"cooking,swimming,adventure,drawing",
"country":"United Kingdom::Edinburgh",
"about":"I often read (and enjoy!) things that are completely opposed to everything I believe. I\'ve enjoyed."
,
"name":"Sisera",
"born":"1962-01-25",
"orientation":"Homosexual",
"purpose":"Flirt",
"tags":"reading,fishing,tennis,theater",
"country":"United States::Fairfield",
"about":"I\'m OCD about shoes being placed side-by-side. I don\'t like them together by the door."
]
提前致谢。
【问题讨论】:
【参考方案1】:你可以用这个
str_replace("'",'"',$jsonstring)
对于在线工具,您可以使用此格式化程序 https://www.freeformatter.com/json-formatter.html#ad-output
正如 super 为 preg_replace 所建议的那样。代码应该是这样的
preg_replace("/',/", '",', preg_replace("/':'/", '":"', preg_replace('/[ \t]+/', ' ', preg_replace("/[\r\n]+\s*[']\s*/", "\n\"", $jsonstring))));
【讨论】:
谢谢。在线工具很酷,正则表达式不起作用。另一个问题是无效的 json 在值中包含单引号,例如“不能,我会,不要” 不管怎样都不行,我更新了无效和有效的json例子,请大家看看好吗?以上是关于是否有任何在线工具可用于将无效 JSON 中的单引号替换为双引号或在 PHP 中使用无效 json 的任何解决方案?的主要内容,如果未能解决你的问题,请参考以下文章
是否有任何用于将 JSON 对象传递给 android 的 PHP 配置?