Emoji characters cannot be encoded to JSON

后端 未结 6 1834
长发绾君心
长发绾君心 2021-01-31 06:29

I have a UITextView which I call messageField. The data within that messageField is POST-ed to server in JSON format. When th

6条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 06:48

    I had the same problem, after digging for hours and finally found this answer that works for me: https://stackoverflow.com/a/8339255/1090945

    If you are using rails as your server, this is all you need to do. No need to do anything in ios/xcode, just pass the NSString without doing any UTF8/16 encoding stuff to the server.

    Postegre stores the code correctly, it's just when you send the json response back to your ios client, assuming you do render json:@message, the json encoding has problem.

    you could test whether you are having json encoding problem in your rails console by doing as simple test:

    test = {"smiley"=>"u{1f604}"} test.to_json

    if it prints out "{\"smiley\":\"\uf604\"}" (notice the 1 is lost), then you have this problem. and the patch from the link will fix it.

提交回复
热议问题