【问题标题】: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?是否有任何在线工具可用于将无效 JSON 中的单引号替换为双引号或在 PHP 中使用无效 json 的任何解决方案?
【发布时间】: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."
    }
]

提前致谢。

【问题讨论】:

    标签: php json single-quotes


    【解决方案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例子,请大家看看好吗?
    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2018-01-07
    • 2022-08-20
    • 2015-02-27
    • 2017-06-19
    • 2015-10-04
    相关资源
    最近更新 更多