Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

c# - How to parse malformed JSONP with hex-encoded characters using JSON.NET?

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":"x3cemx3ebarsx3c/emx3e of sunlight shafting through the broken windows","language":"en"}]}]}

The

x3cemx3ebarsx

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...