我的mysql数据库中有这个JSON编码的代码:
{"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"}}
我想查询并解码它以在我的网站上回复它。
答案 0 :(得分:0)
你必须使用php" json_decode()"用于解码json编码数据的函数。 基本上json_decode()函数将JSON数据转换为PHP数组。
语法:json_decode(data,dataTypeBoolean,depth,options)
数据: - 要在PHP中解码的json数据。
dataTypeBoolean(可选): - 布尔值,如果设置为" true",则使函数返回PHP关联数组,如果省略此参数,则返回PHP stdClass对象或设置为" false"。这两种数据类型都可以像数组一样访问,并使用基于数组的PHP循环进行解析。
深度: - 可选的递归限制。使用整数作为此参数的值。
选项: - 可选的JSON_BIGINT_AS_STRING参数。
现在来到您的代码
$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"}}';
将有效的json数据分配给单个“#”中的变量$ json_string('')as json字符串已经有双重报价。
// 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/>";
}
}