Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a Japanese Character in json_encode?

Tags:

json

sql

utf-8

I'm trying to save a json_encoded strings to the database but whenever i do i get this:

u30abu30bf

when saving:

カタ

how do i decode such a thing?

tried php utf8_decode and encode but i doesn't seem to work, i also tried reading the threads below:

  1. PHP decoding and encoding json with unicode characters

  2. difficulty passing Japanese characters(UTF-8) via json_encode

here is a var_dump data of $flex_encoded before passing it as a parameter:

//this one seems to passing the right encoding
[{"japanese_name":"\u30ca\u30ab\u30cd"},{"japanese_name":"\u30ca\u30ab\u30cd"}]

here is the function call:

$this->updateFlexData($game_id, $flex_encoded);

here is the function for updating my db:

function updateFlexData($game_id, $json_data)
{
    $arrVal = array(
        'json_data'=> $json_data,
    );

    Db::getInstance()->autoExecute('japanese_names', $arrVal, 'UPDATE', " game_id = '".$game_id."'", false);
}

database column type is:

`json_data` longtext CHARACTER SET utf8 COLLATE utf8_bin NOT NULL

my PHP version:

PHP Version 5.3.10

like image 389
Viscocent Avatar asked Apr 03 '14 09:04

Viscocent


1 Answers

Try JSON_UNESCAPED_UNICODE

json_encode($data, JSON_UNESCAPED_UNICODE);

And instead of this \u7537\u6027\u304c\u5f7c\u5973\u306b\u7740\u3066\... you're going to get 男性が彼...

like image 93
whitesiroi Avatar answered Oct 10 '22 00:10

whitesiroi