Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用JSON.NET使用十六进制编码字符解析格式错误的JSONP?_C#_Google Api_Json.net_Jsonp - Fatal编程技术网

C# 如何使用JSON.NET使用十六进制编码字符解析格式错误的JSONP?

C# 如何使用JSON.NET使用十六进制编码字符解析格式错误的JSONP?,c#,google-api,json.net,jsonp,C#,Google Api,Json.net,Jsonp,我调用google的dictionary api,如下所示: 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

我调用google的dictionary api,如下所示:

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)", "");
JObject o = JObject.Parse(json);
遇到以下情况时,解析将终止:

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

\x3cem\x3ebars\x

东西扼杀了解析

有没有办法用JSON.NET处理这个JSONP响应


另一个“Parse JSONP”问题的答案显示了很好的正则表达式
x=regex.Replace(x,@“^.+?\(|\)$”,“”)
处理JSONP部分(在这种情况下可能需要调整regex),因此这里的主要部分是如何处理十六进制编码的字符。

服务器没有返回有效的JSON:JSON不支持
\xAB
字符转义序列,只支持
\uABCD
转义序列

我看到的“解决方案”首先对字符串执行文本替换。这是我的一本书。注意下面的正则表达式
inputString.replaceAll(\\x(\d{2})”,“\\u00$1”)
;适应语言。

参考:

字符串的JSON规范不允许十六进制ASCII转义序列,而只允许Unicode转义序列,这就是转义序列无法识别的原因,也是使用\u0027替代的原因。。。现在,您可以盲目地将\x替换为\u00(这在有效的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)", "")
                .Replace("\\x","\\u00");

        JObject o = JObject.Parse(json);