1

フォローアップ:https ://stackoverflow.com/posts/13303108すべての回答がJSを示唆している

以下を取得し、発生時にphpで修正して、出力がjsonで利用できるようにする、ある種の関数はありますか?フロントエンド(js)はオプションではありません。問題は、配列が引用されていないことにつながる数字か、そのための何かにあるようです。ソースも修正できません。

 {
    29646191: [
        "https://www.facebook.com/RobertScoble/posts/480030845352725",
        "https://sphotos-a.xx.fbcdn.net/hphotos-prn1/s480x480/546943_10151235934049655_1771118951_n.jpg",
        "Today I tried... | Facebook",
        "Robert Scoble wrote: Today I tried something. I paid $49 and... Join Facebook to connect with Robert Scoble and others you may know.",
        null,
        [
            "//images3-focus-opensocial.googleusercontent.com/gadgets/proxy?url\u003dhttps://sphotos-a.xx.fbcdn.net/hphotos-prn1/s480x480/546943_10151235934049655_1771118951_n.jpg\u0026container\u003dfocus\u0026gadget\u003da\u0026rewriteMime\u003dimage/*\u0026refresh\u003d31536000\u0026resize_h\u003d150\u0026resize_w\u003d150\u0026no_expand\u003d1",
            150,
            150,
            null,
            null,
            null,
            null,
            null,
            [
                3,
                "https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?url\u003dhttps://sphotos-a.xx.fbcdn.net/hphotos-prn1/s480x480/546943_10151235934049655_1771118951_n.jpg\u0026container\u003dfocus\u0026gadget\u003dhttps://plus.google.com\u0026rewriteMime\u003dimage/*\u0026resize_h\u003d800\u0026resize_w\u003d800\u0026no_expand\u003d1"
            ]
        ],
        "//s2.googleusercontent.com/s2/favicons?domain\u003dwww.facebook.com",
        [],
        null,
        []
    ]
}

編集:非常に基本的な関数はここでは機能しません(jsonエンコード/デコード)。phpがそれを処理できるようになるには、ソースから取得して多くのことを実行する必要がありました。

つまり、これはこの前にそれを処理するコードです

       $instring = false;
    $inescape = false;
    $lastchar = '';
    $output = "";
    for ( $x=0; $x<strlen( $sourcejson ); $x++ ) {

        $char = substr( $sourcejson, $x, 1 );

        //toss unnecessary whitespace
        if ( !$instring && ( preg_match( '/\s/', $char ) ) ) {
            continue;
        }

        //handle strings
        if ( $instring ) {
            if ( $inescape ) {
                $output .= $char;
                $inescape = false;
            } else if ( $char == '\\' ) {
                $output .= $char;
                $inescape = true;
            } else if ( $char == '"' ) {
                $output .= $char;
                $instring = false;
            } else {
                $output .= $char;
            }
            $lastchar = $char;
            continue;
        }


        switch ( $char ) {

            case '"':
                $output .= $char;
                $instring = true;
                break;

            case ',':
                if ( $lastchar == ',' || $lastchar == '[' || $lastchar == '{' ) { 
                    $output .= 'null';
                }
                $output .= $char;
                break;

            case ']':
            case '}':
                if ( $lastchar == ',' ) { 
                    $output .= 'null';
                }
                $output .= $char;
                break;

            default:
                $output .= $char;
                break;
        }
        $lastchar = $char;
    }
4

2 に答える 2

1

自分でjsonをエンコード/デコードしようとしないでください。

PHP 配列から json をエンコードするには:

echo json_encode($data);

json をデコードするには:

var_dump(json_decode($data));

あなたの場合、INTを文字列に変換する必要があります。これを試して:

# this solution requires PHP >= 5.4.0
var_dump(json_decode($data, false, 512, JSON_BIGINT_AS_STRING));

詳細については、 を参照json_decode()してください


編集

それでも問題が解決しない場合は、PHP スクリプトで処理する前に、JavaScript で有効な JSON を取得することを強く検討してください。

// sadly this is the only solution i can find; luckily, your json is coming from a trusted source (Google), so eval shouldn't be an issue;
jsonObject = eval('(' + invalidJsonString + ')');

// conver the object back to json string
validJsonString = JSON.stringify(jsonObject);

// yay, valid json
console.log(validJsonString);

jsfiddleで動作するのを見てください

免責事項

jQuery.parseJSON(jsonString)通常はorを使用しますJSON.parse(jsonStringが、これらはその int をオブジェクトのキーとして解析エラーを出しています :(

于 2012-11-24T22:17:39.100 に答える
0

を使用する必要がありますjson_decode();

これを試して

$data[] = //some json;
 var_dump(json_decode($data));

その後、使用できます

json_last_error()

最終的なエラーを確認する

参照ドキュメントを参照してください: http://www.php.net/manual/en/function.json-decode.php

于 2012-11-24T22:17:21.080 に答える