正则表达式帮助PHP获得有效的JSON

时间:2018-03-30 05:55:13

标签: php regex

我有一个无效的json字符串。我想将该字符串转换为有效的json。有效的json需要双引号。

这是字符串

{
                $page: $('.js-profile'),
                id: 242144,
                docName: Mr. test,
                gender: test,
                testid: -1,
                testPlanId: -1,
                tesstspecialty: doctor,
                date: null,
                isPatientExisting: false,                
                constraints: {},
                culture: en,
                isMobile: false,
                isIE8: false,
                isLocked: false,
                isUserLoggedIn: false,
                startTime: 3/30/2018,
                dateFormatString: {0}-{1}-{2},
                searchUrl: /search?address=10003&dr_specialty=99&match_insurance=on,
                locationId: 87800,
                test : {
                test: 1, test:6
                }
            }

任何人都可以帮助制作将双引号添加到字符串中的reg表达式吗?

任何形式的帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

在一个正则表达式中很难做到,但很容易转换第一部分,然后在值中添加引号:

<?php
$invalidJson = <<<json
{
                \$page: $('.js-profile'),
                id: 242144,
                docName: Mr. test,
                gender: test,
                testid: -1,
                testPlanId: -1,
                tesstspecialty: doctor,
                date: null,
                isPatientExisting: false,                
                constraints: {},
                culture: en,
                isMobile: false,
                isIE8: false,
                isLocked: false,
                isUserLoggedIn: false,
                startTime: 3/30/2018,
                dateFormatString: {0}-{1}-{2},
                searchUrl: /search?address=10003&dr_specialty=99&match_insurance=on,
                locationId: 87800,
                test : {
                test: 1, test:6
                }
            }
json;
$validJson = preg_replace("/([a-z0-9$]+)\s*:/i", '"$1":', $invalidJson); // Replace the first part
$validJson = str_replace("{}", "{\n}", $validJson); // Make sure {} will be treated as an empty array
$validJson = preg_replace("/:\s*([^,\n]+),/i", ': "$1",', $validJson); // Quote the values
var_dump(json_decode($validJson, true));

答案 1 :(得分:0)

我已经尝试过艰难的路线并试图一次性修复它:

This也会保留null等不需要引用的数据类型:

([\$\w]+)\s*: ?(?|(\d{1,2}\/\d{1,2}\/\d{2,4})|(\{[^{\n-]+\}-?\{[^{\n-]+\}-?\{[^{\n-]+\}-?)+|(?(?!null|false|true|-?\d+|{\s*})([^{\n,]*)))

preg_replace "$1": "$2"之后,只需删除不必要的双引号:

<?php
$re = '/([\$\w]+)\s*: ?(?|(\d{1,2}\/\d{1,2}\/\d{2,4})|(\{[^{\n-]+\}-?\{[^{\n-]+\}-?\{[^{\n-]+\}-?)+|(?(?!null|false|true|-?\d+|{\s*})([^{\n,]*)))/m';
$str = '{
    $page: $(\'.js-profile\'),
    id: 242144,
    docName: Mr. test,
    gender: test,
    testid: -1,
    testPlanId: -1,
    tesstspecialty: doctor,
    date: null,
    isPatientExisting: false,                
    constraints: {},
    culture: en,
    isMobile: false,
    isIE8: false,
    isLocked: false,
    isUserLoggedIn: false,
    startTime: 3/30/2018,
    dateFormatString: {0}-{1}-{2},
    searchUrl: /search?address=10003&dr_specialty=99&match_insurance=on,
    locationId: 87800,
    test : {
    testa: 1, testb:6.4
    }
}';
$subst = '"$1": "$2"';    
$result = preg_replace($re, $subst, $str);    
echo str_replace('""', '', $result);

结果看起来很不错我会说:

{
    "$page": "$('.js-profile')",
    "id": 242144,
    "docName": "Mr. test",
    "gender": "test",
    "testid": -1,
    "testPlanId": -1,
    "tesstspecialty": "doctor",
    "date": null,
    "isPatientExisting": false,                
    "constraints": {},
    "culture": "en",
    "isMobile": false,
    "isIE8": false,
    "isLocked": false,
    "isUserLoggedIn": false,
    "startTime": "3/30/2018",
    "dateFormatString": "{0}-{1}-{2}",
    "searchUrl": "/search?address=10003&dr_specialty=99&match_insurance=on",
    "locationId": 87800,
    "test": {
    "testa": 1, "testb": 6.4
    }
}