如何在PHP中更正无效的JSON? [英] How to correct an invalid JSON in php?

查看:62
本文介绍了如何在PHP中更正无效的JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题. 犯了一个错误,我有很多无效的JSON字符串,如下所示:

I have a problem. For a mistake I have a lot of not valid JSON strings like this:

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=u0027non supporting iframesu0027&Market=u0027it-ITu0027&Adult=u0027Offu0027&Options=u0027DisableLocationDetectionu0027&WebSearchOptions=u0027DisableQueryAlterationsu0027&$skip=0&$top=1",
                    "type": "WebResult"
                },
                "ID": "7858fc9f-6bd5-4102-a835-0fa89e9f992a",
                "Title": "something good",
                "Description": "something "WRONG" here!",
                "DisplayUrl": "www.devx.com/Java/Article/27685/1954",
                "Url": "http://www.devx.com/Java/Article/27685/1954"
            }
        ],
        "__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=u0027non%20supporting%20iframesu0027&Market=u0027it-ITu0027&Adult=u0027Offu0027&Options=u0027DisableLocationDetectionu0027&WebSearchOptions=u0027DisableQueryAlterationsu0027&$skip=50"
    }
}

如您所见,Description字段包含一个错误的字符串(插入"),因此我无法使用php的json_decode解析json,实际上它返回NULL. 我有100万个错误的json,比这个错误大得多(10倍). 我该如何在php中做

As you can see the field Description contains a bad string (" into "), so I'm not able to parse the json using php's json_decode, infact it returns NULL. I've 1 million of wrong json, much more big than this (10 times). How can I do in php?

推荐答案

在您的情况下,您可以利用json中的字符串不能超过一行的事实.这是捕捉多行感知搜索并替换为正则表达式函数(例如 preg_match_callback )的紧要点.在PHP中.

In your case you could exploit the fact that strings in json could not be over a line. That is a snappy point to grab with s multi-line aware search and replace with a regular expression function like preg_match_callback in PHP.

 /^\s+"[a-z_"]+": "([^"]*".*)",?$/mi

该行开头的空白;成员名称,以有效名称的形式(此处仅包含字符和下划线)作为字符串; :,然后是断开的字符串,直到行末 (可选),后跟逗号,?.

Whitespace at the beginning of the line; member-name in form of a valid name (only characters and underscore here) as a string; the : and then the broken string until the end of the line optionally followed by a comma ,?.

此正则表达式仅与无效行匹配.但是,如果您的json还包含一个内部带有\"的有效字符串,则此正则表达式不能真正起作用.

This regex already matches only invalid lines. However if your json also contains a valid string with \" inside, this regex does not really work.

因此,最好检查一下替换项是否可以达到预期的效果.

So it's also good to place some checks that the replacement would do what it is intended.

$like = '... json-like but broken json string as in question ...';

// Fixing #1: member strings containing double-quotes on the same line.

$fix1Pattern   = '/^(\s+"[a-z_]+": ")([^"]*".*)(",?)$/mi';

$fix1Callback  = function ($matches) {
    list($full, $prefix, $string, $postfix) = $matches;
    $fixed = strtr($string, ['"' => '\"']);
    if (!is_string(json_decode("\"$fixed\""))) {
        throw new Exception('Fix #1 did not work as intended');
    }
    return "$prefix$fixed$postfix";
};


// apply fix1 onto the string

$buffer = preg_replace_callback($fix1Pattern, $fix1Callback, $like);


// test if it finally works

print_r(json_decode($buffer));

请记住,这是有限的.您可能需要首先了解正则表达式,这是它自己的世界.但是原理通常非常相似:您在字符串中搜索作为损坏部分的模式,然后进行一些字符串操作来解决这些问题.

Keep in mind that this is limited. You might need to learn about regular expressions first which is a world of it's own. But the principle is often very similar: You search the string for the patterns that are the broken parts and then you do some string manipulation to fix these.

如果json字符串更残破,那么这需要更多的爱,可能仅凭正则表达式就不容易解决.

If the json string is much more broken, then this needs even more love, probably not to be easily solved with a regular expression alone.

代码示例和提供的数据的示例输出:

Exemplary output for the code-example and the data provided:

stdClass Object
(
    [d] => stdClass Object
        (
            [results] => Array
                (
                    [0] => stdClass Object
                        (
                            [__metadata] => stdClass Object
                                (
                                    [uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=u0027non supporting iframesu0027&Market=u0027it-ITu0027&Adult=u0027Offu0027&Options=u0027DisableLocationDetectionu0027&WebSearchOptions=u0027DisableQueryAlterationsu0027&$skip=0&$top=1
                                    [type] => WebResult
                                )

                            [ID] => 7858fc9f-6bd5-4102-a835-0fa89e9f992a
                            [Title] => something good
                            [Description] => something "WRONG" here!
                            [DisplayUrl] => www.devx.com/Java/Article/27685/1954
                            [Url] => http://www.devx.com/Java/Article/27685/1954
                        )

                )

            [__next] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=u0027non%20supporting%20iframesu0027&Market=u0027it-ITu0027&Adult=u0027Offu0027&Options=u0027DisableLocationDetectionu0027&WebSearchOptions=u0027DisableQueryAlterationsu0027&$skip=50
        )

)

这篇关于如何在PHP中更正无效的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆