我有一个多维数组。我想将该数组转换为JSON。我正在使用[global::System.Data.Linq.Mapping.TableAttribute(Name= "LeumiApp.dbo.CategoryVariants")]
,但无法正常工作。
这是我正在使用的代码。
json_encode
**下面的函数已写在helper_functions.php中**
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
header('Content-Type: application/json');
include '../panel/config/conn.php';
include '../panel/config/helper_functions.php';
$postions = array(1 => "Goalkeeper", 2 => "Defender", 3 => "Midfielder", 4 => "Forward");
$result = array();
$tmp = get_football_assets($conn);
foreach ($tmp as $key => $value) {
$pair = strtolower($value['pair']);
$value['pair'] = $pair;
$value['position'] = $postions[$value['position_id']];
unset($value['position_id']);
$result[$pair] = $value;
}
echo json_encode($result);
如果我执行function get_football_assets($con){
$data = array();
$sql = "SELECT nationality, birthdate, birthcountry, birthplace, height, weight, players.position_id as position_id, players.image_path as image_path, teams.name as club, countries.name as country, feed.pair as pair, feed.fullname as name, feed.price as price from players JOIN feed ON feed.player_id = players.api_id JOIN teams ON teams.api_id = players.team_id JOIN countries ON countries.api_id = players.country_id";
mysqli_set_charset($con, 'utf8');
$result = mysqli_query($con, $sql);
while($rows = mysqli_fetch_assoc($result)){
$data[] = $rows;
}
return $data;
}
,则它显示以下结果,但由于输出具有1500多个记录,所以我仅显示3条记录:-
print_r($result)
[sergeaurier95094] => Array
(
[nationality] => C�te d'Ivoire
[birthdate] => 24/12/1992
[birthcountry] => C�te d'Ivoire
[birthplace] => Ouragahio
[height] => 176 cm
[weight] => 76 kg
[image_path] => https://cdn.sportmonks.com/images/soccer/players/22/95094.png
[club] => Tottenham Hotspur
[country] => C�te d'Ivoire
[pair] => sergeaurier95094
[name] => Serge Aurier
[price] => 10.00
[position] => Defender
),
[davinsons�nchezmina26294] => Array
(
[nationality] => Colombia
[birthdate] => 12/06/1996
[birthcountry] => Colombia
[birthplace] => Caloto
[height] => 187 cm
[weight] => 81 kg
[image_path] => https://cdn.sportmonks.com/images/soccer/players/22/26294.png
[club] => Tottenham Hotspur
[country] => Colombia
[pair] => davinsons�nchezmina26294
[name] => Davinson S�nchez Mina
[price] => 10.00
[position] => Defender
),
[lucasrodriguesmouradasilva95998] => Array
(
[nationality] => Brazil
[birthdate] => 13/08/1992
[birthcountry] => Brazil
[birthplace] => S�o Paulo
[height] => 172 cm
[weight] => 72 kg
[image_path] => https://cdn.sportmonks.com/images/soccer/players/30/95998.png
[club] => Tottenham Hotspur
[country] => Brazil
[pair] => lucasrodriguesmouradasilva95998
[name] => Lucas Rodrigues Moura da Silva
[price] => 10.00
[position] => Midfielder
)
返回的 UTF-8格式错误的字符,可能编码错误。
修改
我已按照建议删除了array_map并使用了echo json_last_error_msg();
,但是它无法转换 alissonrams.sbecker129820,davinsons.nchezmina26294,nabydecoke.ta32640等字符。
mysqli_set_charset($con, 'utf8');
实际上,问题出在echo bin2hex($pair); gives me the following output :-
6875676f6c6c6f72697331303134
6d696368656c766f726d393737
746f6279616c64657277656972656c64393838
73657267656175726965723935303934
62656e646176696573393839
64616e6e79726f736531323731
646176696e736f6e73e3816e6368657a6d696e613236323934
6b696572616e747269707069657231303136
6a616e766572746f6e6768656e393135
6b796c6577616c6b65722d70657465727334363539
62616d6964656c65616c6c69393232
63687269737469616e64616e6e656d616e6e6572696b73656e393932
6572696364696572393136
6572696b6c616d656c61393431
6c75636173726f647269677565736d6f757261646173696c76613935393938
6d6f75737361736973736f6b6f31333830
686172727977696e6b7331373131
68617272796b616e65393937
6665726e616e646f6c6c6f72656e7465746f7272657331363332
6865756e672d6d696e736f6e34333133
6c756b65616d6f733130363830
7061756c6f64696e6f67617a7a616e69676131343131
766963746f7277616e79616d6131313732
6a75616e6d6172636f73666f797468333335373035
616c66696577686974656d616e333430333436
67656f7267656d61727368353430303835
上。因此,对我来说效果很好的更新代码如下:-
strtolower