sql >> データベース >  >> RDS >> Mysql

PHPで正しくフォーマットされていないJSONを修正するにはどうすればよいですか?

    私は独自のjsonFixer()関数を持っています-それは2つのステップで機能します:ゴミの除去(一貫性のないフォーマットの同等性のため)と再フォーマットです。

    <?php
      function jsonFixer($json){
        $patterns     = [];
        /** garbage removal */
        $patterns[0]  = "/([\s:,\{}\[\]])\s*'([^:,\{}\[\]]*)'\s*([\s:,\{}\[\]])/"; //Find any character except colons, commas, curly and square brackets surrounded or not by spaces preceded and followed by spaces, colons, commas, curly or square brackets...
        $patterns[1]  = '/([^\s:,\{}\[\]]*)\{([^\s:,\{}\[\]]*)/'; //Find any left curly brackets surrounded or not by one or more of any character except spaces, colons, commas, curly and square brackets...
        $patterns[2]  =  "/([^\s:,\{}\[\]]+)}/"; //Find any right curly brackets preceded by one or more of any character except spaces, colons, commas, curly and square brackets...
        $patterns[3]  = "/(}),\s*/"; //JSON.parse() doesn't allow trailing commas
        /** reformatting */
        $patterns[4]  = '/([^\s:,\{}\[\]]+\s*)*[^\s:,\{}\[\]]+/'; //Find or not one or more of any character except spaces, colons, commas, curly and square brackets followed by one or more of any character except spaces, colons, commas, curly and square brackets...
        $patterns[5]  = '/["\']+([^"\':,\{}\[\]]*)["\']+/'; //Find one or more of quotation marks or/and apostrophes surrounding any character except colons, commas, curly and square brackets...
        $patterns[6]  = '/(")([^\s:,\{}\[\]]+)(")(\s+([^\s:,\{}\[\]]+))/'; //Find or not one or more of any character except spaces, colons, commas, curly and square brackets surrounded by quotation marks followed by one or more spaces and  one or more of any character except spaces, colons, commas, curly and square brackets...
        $patterns[7]  = "/(')([^\s:,\{}\[\]]+)(')(\s+([^\s:,\{}\[\]]+))/"; //Find or not one or more of any character except spaces, colons, commas, curly and square brackets surrounded by apostrophes followed by one or more spaces and  one or more of any character except spaces, colons, commas, curly and square brackets...
        $patterns[8]  = '/(})(")/'; //Find any right curly brackets followed by quotation marks...
        $patterns[9]  = '/,\s+(})/'; //Find any comma followed by one or more spaces and a right curly bracket...
        $patterns[10] = '/\s+/'; //Find one or more spaces...
        $patterns[11] = '/^\s+/'; //Find one or more spaces at start of string...
    
        $replacements     = [];
        /** garbage removal */
        $replacements[0]  = '$1 "$2" $3'; //...and put quotation marks surrounded by spaces between them;
        $replacements[1]  = '$1 { $2'; //...and put spaces between them;
        $replacements[2]  = '$1 }'; //...and put a space between them;
        $replacements[3]  = '$1'; //...so, remove trailing commas of any right curly brackets;
        /** reformatting */
        $replacements[4]  = '"$0"'; //...and put quotation marks surrounding them;
        $replacements[5]  = '"$1"'; //...and replace by single quotation marks;
        $replacements[6]  = '\\$1$2\\$3$4'; //...and add back slashes to its quotation marks;
        $replacements[7]  = '\\$1$2\\$3$4'; //...and add back slashes to its apostrophes;
        $replacements[8]  = '$1, $2'; //...and put a comma followed by a space character between them;
        $replacements[9]  = ' $1'; //...and replace by a space followed by a right curly bracket;
        $replacements[10] = ' '; //...and replace by one space;
        $replacements[11] = ''; //...and remove it.
    
        $result = preg_replace($patterns, $replacements, $json);
    
        return $result;
      }
    ?>
    

    使用例:

    <?php
      // Received badly formatted json:
      // {"contact1": "David "Dave" Letterman", price : 30.00, 'details' : "Greatest 'Hits' Album"}
      $json_string = '{"contact1": "David "Dave" Letterman", price : 30.00, \'details\' : "Greatest \'Hits\' Album"}';
      jsonFixer($json_string);
    ?>
    

    結果:

    {"contact1": "David \"Dave\" Letterman", "price" : "30.00", "details" : "Greatest \'Hits\' Album"}
    

    注:これは、正しくフォーマットされていない可能性のあるすべてのJSON文字列でテストされたわけではありませんが、複雑なマルチレベルのJSON文字列で使用しており、それまでは正常に機能しています。



    1. Jquery、PHP、Mysqlを使用したAjaxページ付け

    2. Google Cloud Platform(GCP)でのMySQLフェイルオーバーとフェイルバックの実現

    3. hibernateでネイティブSQLを実行する

    4. データベースから読み取ったオプションメニューを選択し、その値を使用します