1

A follow up to: https://stackoverflow.com/posts/13303108 where all the responses suggested JS

Is there a function of some sort to take the following and fix it with php when it occurs so that the output will be vaild json? front end (js) is not an option. The issue seems to be the number leading into the array not being quoted or something to that end. I cannot fix the source either.

 {
    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,
        []
    ]
}

Edit: the very base functions do not work here (json encode/decode). It had to have been taken from a source and run through many things for php to even start to be able to handle it.

To wit, this is the code that handles it before this

       $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

Do not try to encode/decode json on your own.

To encode json from a PHP array:

echo json_encode($data);

To decode json:

var_dump(json_decode($data));

In your case, you need to convert the INT to a String. Try this:

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

See json_decode() for more information


EDIT

If this still isn't working for you, I'd highly consider get valid JSON in JavaScript before processing it with your PHP script.

// 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);

See it working on jsfiddle

Disclaimer

Normally you'd use jQuery.parseJSON(jsonString) or JSON.parse(jsonString, but these are giving a parse error with that int as a key in the object :(

于 2012-11-24T22:17:39.100 回答
0

you should use the json_decode();

try this

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

then you can use

json_last_error()

to check eventual errors

see the reference doc: http://www.php.net/manual/en/function.json-decode.php

于 2012-11-24T22:17:21.080 回答