Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

how can I Deserialize emoji in json in C#

I have a json file that include emoji when I want to deserialize it , it could not deserialize emoji to string. my code is:

var mystring ={"message":"jjasdajdasjdj laslla aasdasd ssdfdsf!!! ??u{1F3FD}", "updated_time":"2015-04-14T22:37:13+0000", "id":"145193995506_148030368559"}

FaceBookIdea ideaDetails = JsonConvert.DeserializeObject<FaceBookIdea>((mystring).ToString());

the error is :

{"Input string was not in a correct format."}

when I remove emoji it works well.

Thank a lot for your help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your problem is that this portion of your message string does not conform to the JSON standard:

"u{1F3FD}"

According to the standard, u four-hex-digits represents a unicode character literal given by the hex value of its code point. Your string u{1F3FD} with its curly braces does not conform to this convention, and so Json.NET throws an exception upon trying to parse it. You will see a similar error if you upload your JSON to https://jsonformatter.curiousconcept.com/.

Thus it would seem, to fix your JSON to make it conform to the standard, you need to format your character like uXXXX using the appropriate 4 hex digits. However, your character, U+1F3FD, is larger than 0xFFFF and does not exist on the Unicode Basic Multilingual Plane. It cannot be represented as a single 4-digit hex number. c# (and utf-16 in general) represents such Unicode characters as surrogate pairs -- pairs of two two-byte chars. You will need to do the same here. The UTF-16 (hex) representation of your character is

0xD83C 0xDFFD 

Thus your JSON character needs to be:

uD83CuDFFD

And for your entire string:

{"message":"jjasdajdasjdj laslla aasdasd ssdfdsf!!! ??uD83CuDFFD", "updated_time":"2015-04-14T22:37:13+0000", "id":"145193995506_148030368559"}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...