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

Community
  • 1
  • 1
Viscocent
  • 2,024
  • 2
  • 19
  • 26
  • can you show us the code you got please – CodeFanatic Apr 03 '14 at 09:30
  • I'd guess this is a combination of [Reference: Why are my “special” Unicode characters encoded weird using json_encode?](http://stackoverflow.com/questions/22745662/reference-why-are-my-special-unicode-characters-encoded-weird-using-json-enco) plus you're somehow stripping out the backslashes somewhere. – deceze Apr 03 '14 at 09:33
  • and can you tell us your php version pls – CodeFanatic Apr 03 '14 at 09:36
  • @FelixLahmer just updated the thread with some info you requested, sorry it took some time. i left from work after posting this. – Viscocent Apr 04 '14 at 00:57
  • @deceze - yes its somehow stripping the backlashes i do think its the problem with the update function. – Viscocent Apr 04 '14 at 00:58
  • I believe you need to read http://kunststube.net/escapism. – deceze Apr 04 '14 at 05:39

2 Answers2

14

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 男性が彼...

whitesiroi
  • 2,725
  • 4
  • 30
  • 64
-1

after hours of tinkering with the code i somehow made it to work: ok its maybe not the solution but:

  1. when i proceed to storing json_data with this content(below) decoding json_data from database results to parse error because of the extra " quotations

    $japanese_name = json_encode($japanese_text); //returns "\u30ca\u30ab\u30cd"
    
  2. i just removed the " from each sides to make it just \u30ca\u30ab\u30cd and then proceeded to store in the db

    $jp = str_replace("\"","",$japanese_name); //returns \u30ca\u30ab\u30cd
    
Viscocent
  • 2,024
  • 2
  • 19
  • 26
  • -1 Your only problem is that you're not escaping the data correctly for use in an SQL statement. – deceze Apr 04 '14 at 08:18