如何解码嵌入在json字符串中的HTML编码字符 [英] How to decode HTML encoded character embedded in a json string

查看:94
本文介绍了如何解码嵌入在json字符串中的HTML编码字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对从JSon结果中解码特殊字符有一个小问题(在我的情况下为\ x27,但它可以是任何有效的html编码字符).如果结果中不包含任何转义字符,则可以正常工作,但如果不成功,则会收到无法识别的转义序列异常.在尝试使用JavascriptSerializer反序列化之前,我尝试在Json字符串上执行HttpUtility.HtmlDecode,它不起作用,字符仍然处于编码格式.

I've a little question about decoding special characters from a JSon result (in my case, \x27 but it could be any valid html encoded character). If the result doesn't contains any escaped characters, it works well but if not, I get a Unrecognized escape sequence exception. I try to do an HttpUtility.HtmlDecode on the Json string before deserializing using JavascriptSerializer, it doesn't work, the character still in encoded format.

这是一个代码段:

public IEnumerable<QuoteInfo> ParseJson(string json)
{
    System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
    List<QuoteInfo> result = jss.Deserialize<List<QuoteInfo>>(System.Web.HttpUtility.HtmlDecode(json));
    return result;
}

我试图使用RegistersConverters对HtmlDecode进行反序列化期间可以找到的任何字符串进行解码,但是我不知道如何正确使用它.

I tried to use RegistersConverters to HtmlDecode any string I could find during deserialization but I can't figure out how to use it properly.

我该如何解决这个问题?

How can I solve that problem?

正如back2dos很好地解释的那样,此问题与HtmlDecode问题无关,而与格式错误的Json字符串有关.

As back2dos nicely explained, this problem wasn't related to an HtmlDecode problem but to an misformatted Json string.

推荐答案

好,我对C#有非常肤浅的了解,而对.NET API则一无所知,但是直观地HtmlDecode应该解码

ok, i have very superficial knowledge about C#, and none about the .NET API, but intuitively HtmlDecode should decode HTML entities (please excuse me if i'm wrong on that one) ... encoding is quite a b*tch, i know, so i will try to clearly explain the differences between what you have, what you tried, and what should work ...

正确的 HTML 实体将是&#x27,而不是\x27 ... \x27是十六进制的 ASCII >转义序列,已被某些JSON解码器和许多编程语言接受,但 HTML ...

the correct HTML entity would be &#x27 and not \x27 ... \x27 is a hexadecimal ASCII escape-sequence, as accepted by some JSON decoders and many programming languages, but is completely unrelated to HTML ...

,而且,它与JSON无关,这就是问题所在.

and also, it has nothing to do with JSON, which is the problem ... JSON specs for strings do not allow hexadecimal ASCII escape-sequences, but only Unicode escape-sequences, which is why the escape sequence is unrecognized and which is why using \u0027 instead should work ... now you could blindly replace \x with \u00 (this should perfectly work on valid JSON, although some comments may get damaged in theory, but who cares ... :D)

但就个人而言,如果您有权访问源,则应该对其进行修改,以使其输出有效 JSON以符合规范...

but personally, if you have access to the source, you should modify it, to make it output valid JSON to match the specs ...

greetz

back2dos

这篇关于如何解码嵌入在json字符串中的HTML编码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆