如何使用JSON.NET以十六进制编码的字符分析格式错误的JSONP? [英] How to parse malformed JSONP with hex-encoded characters using JSON.NET?

查看:220
本文介绍了如何使用JSON.NET以十六进制编码的字符分析格式错误的JSONP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var json = new WebClient()。DownloadString(string。格式(@http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q={0}&sl=en&tl=en,bar)); 

然而,我得到的回应是这段代码无法正确解析:

  json = json.Replace(dict_api.callbacks.id100(,).Replace(,200,null),); 
JObject o = JObject.Parse(json);

在遇到这个问题时,解析会消失:

  条目:[{ 类型: 例如, 术语:[{ 类型: 文本, 文本:\x3cem\x3ebars\\ \\ x3c / em \ x3e阳光通过破碎窗户的轴系,language:en}]}]} 


$ b


\x3cem\x3ebars\x




杀死解析的内容



有没有办法通过JSON.NET处理这个JSONP响应?



通过回答用户/ 82208 / aquinas> aquinas 到另一个Parse JSONP问题显示了很好的正则表达式 x = Regex.Replace(x,@^。+?\(| \)$ ,); 用JSONP部件处理(可能需要为这种情况调整正则表达式),所以这里的主要部分是如何处理十六进制编码的字符。

解决方案

参考:




  • 字符串的JSON规范不允许使用十六进制ASCII转义序列,而只使用Unicode转义序列,这就是转义序列无法识别的原因这就是为什么使用\\\'应该工作......现在你可以盲目地用\00替换\x(这应该完全适用于有效的JSON,尽管某些评论可能在理论上受到损害,但是谁在乎...... :D)

    所以改变你的代码就可以解决它:

      var json = new WebClient()。DownloadString(string.Format(@http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q= {0}& sl = en& tl = en,bar)); 

    json = json
    .Replace(dict_api.callbacks.id100(,)
    .Replace(,200,null),)
    。更换(\\x,\\u00);

    JObject o = JObject.Parse(json);


    I make a call to google's dictionary api like this:

    var json = new WebClient().DownloadString(string.Format(@"http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q={0}&sl=en&tl=en", "bar"));
    

    However I get a response that this code fails to parse correctly:

    json = json.Replace("dict_api.callbacks.id100(", "").Replace(",200,null)", "");
    JObject o = JObject.Parse(json);
    

    The parse dies at encountering this:

    "entries":[{"type":"example","terms":[{"type":"text","text":"\x3cem\x3ebars\x3c/em\x3e of sunlight shafting through the broken windows","language":"en"}]}]}
    

    The

    \x3cem\x3ebars\x

    stuff kills the parse

    Is there some way to handle this JSONP response with JSON.NET?

    The answer by aquinas to another "Parse JSONP" question shows nice regex x = Regex.Replace(x, @"^.+?\(|\)$", ""); to handle with JSONP part (may need to tweak regex for this case), so main part here is how to deal with hex-encoded characters.

    解决方案

    Reference: How to decode HTML encoded character embedded in a json string

    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)

    So change your code to this will fix it:

            var json = new WebClient().DownloadString(string.Format(@"http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q={0}&sl=en&tl=en", "bar"));
    
            json = json
                    .Replace("dict_api.callbacks.id100(", "")
                    .Replace(",200,null)", "")
                    .Replace("\\x","\\u00");
    
            JObject o = JObject.Parse(json);
    

    这篇关于如何使用JSON.NET以十六进制编码的字符分析格式错误的JSONP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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