DataContractJsonSerializer not deserializing html entities
I am receiving data from a web service, and some of the strings have html entities in them, for example:
{"prop": "htmlentity - é"}
The é
is not being parsed to é.
My question is twofold:
- Is this even supposed to happen? I looked through the JSON spec the best I could, but couldn't find any reference to html entities.
- What is the right way to do this with a
DataContractJsonSerializer
?,开发者_如何学Go if there is a right way?
You can call HttpUtility.HtmlDecode on the strings that contain HTML entities.
This is not the job of DataContractJsonSerializer, as the JSON spec only requires quotation mark, reverse solidus, and the control characters to be escaped.
This isn't a JSON serialization issue, this will be due to the data being sent over the web.
Serialization does not automatically encode HTML entities.
See:
var orig = new MyObj {prop = "htmlentity - é"};
var ser = new DataContractJsonSerializer(typeof(MyObj));
var ms = new MemoryStream();
ser.WriteObject(ms, orig);
var serialized = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
MessageBox.Show(serialized); // {"prop":"htmlentity - é"}
If you have control of the web service then you can verify this on the server side. If not, check with the provider of the web service.
精彩评论