drr7731 2013-05-10 16:13
浏览 65
已采纳

如何转换json数据?

I've got json data. There is "cyrillic" strings in json file, like this one:

\u0418\u0432\u0430\u043D\u043E\u0432 \u0418.

When I decode json and put this data in database table I get the string

Иванов И.

On one decoding web-site I entered this string and got very good (the one I need)

Иванов И.

And also this site told me that it was converted from CP1252 to UTF-8. So I tried to convert data from json after decoding manually using

mb_convert_encoding ( $string, "UTF-8","windows-1252");
mb_convert_encoding ( $string, "UTF-8","CP1252");

and

iconv("windows-1252","UTF-8",$string);
iconv("CP1252","UTF-8",$string);

Any of this functions made the string in database table look like

Øòðýþò ÃËœ.

or

Øòðýþò Ø.

both are not decoded on above site properly. So the question is, how do I convert this string?

Upd: used this sql request:

ALTER DATABASE logenterprise
    CHARACTER SET utf8

Tried after the same things that wrote above - result is the same. Also tried this just in case:

alter table mytable convert to CHARACTER SET utf8 COLLATE utf8_unicode_ci;
  • 写回答

1条回答 默认 最新

  • dqifn68206 2013-05-10 21:18
    关注

    Curse you damned encodings ^^ They gave me a hard time too.

    Everything looked fine (database, encoding of the inputdata and on the website), but still i got cryptic chars in my tables. So what's the problem then? It's the connection to your database-server.

    Fortunately you can fix this with a simple query.

    Right after establishing the mysql-connection you need to execute the following query:

    mysql_query("SET NAMES 'utf8'");
    

    Voilà. When you execute your INSERT-Query the data gets nicely saved in your db.

    This saved my ass many times as i was handling 'Umlauts' and the €-sign.


    Note: You shouldn't use mysql_xxx methods anymore as they are deprecated. I just used them in the example to make the code clearer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!