普通网友 2014-09-17 18:02
浏览 58
已采纳

查询和解码json形式的mysql数据库

I have this JSON encoded code in my mysql database:

{"Suggestion":{"Title":"Casinos","Text":"maybe it will be good if its there casinos "},"ID":6,"VoteNo":[],"Status":"Voting","Player":{"SteamID":"STEAM_0:1:36988062","Name":"Pepi"},"Approved":{"Name":"Nido Johnson","Is":true,"SteamID":"STEAM_0:0:47457253"},"VoteYes":{"1":"STEAM_0:0:56939043","2":"STEAM_0:0:55948188","3":"STEAM_0:1:25856984","4":"STEAM_0:1:40894071"}}

And i want to query and decode it to echo it at my website.

  • 写回答

1条回答 默认 最新

  • doupinwan0563 2014-09-17 18:06
    关注

    You have to use a php "json_decode()" function to decode a json encoded data. Basically json_decode() function converts JSON data to a PHP array.

    Syntax: json_decode( data, dataTypeBoolean, depth, options )

    data : - The json data that you want to decode in PHP.

    dataTypeBoolean(Optional) :- boolean that makes the function return a PHP Associative Array if set to "true", or return a PHP stdClass object if you omit this parameter or set it to "false". Both data types can be accessed like an array and use array based PHP loops for parsing.

    depth :- Optional recursion limit. Use an integer as the value for this parameter.

    options :- Optional JSON_BIGINT_AS_STRING parameter.

    Now Comes to your Code

        $json_string = '{"Suggestion":{"Title":"Casinos","Text":"maybe it will be good if its there casinos "},"ID":6,"VoteNo":[],"Status":"Voting","Player":{"SteamID":"STEAM_0:1:36988062","Name":"Pepi"},"Approved":{"Name":"Nido Johnson","Is":true,"SteamID":"STEAM_0:0:47457253"},"VoteYes":{"1":"STEAM_0:0:56939043","2":"STEAM_0:0:55948188","3":"STEAM_0:1:25856984","4":"STEAM_0:1:40894071"}}';
    

    Assign a valid json data to a variable $json_string within single quot's ('') as json string already have double quots.

    // here i am decoding a json string by using a php 'json_decode' function, as mentioned above & passing a true parameter to get a PHP associative array otherwise it will bydefault return a PHP std class objecy array.
    
        / just can check here your encoded array data.
    // echo '<pre>';
    // print_r($json_decoded_data);
    
    // loop to extract data from an array
    foreach ($json_decoded_data as $key => $value) {
        echo "$key <br/>";
        foreach($value as $k=>$data)
        {
            echo "$k | $data <br/>";
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?