开发者

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:

  1. 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.

  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜