Unexpected character encountered while parsing value: j. Path '', line 0, position 0

那年仲夏 提交于 2019-12-20 05:50:58

问题


t fails to deserialize/prase this json, ive tried multiple combinations with different methods to try and make this work but nothing seems to do it...

The code im using

WebClient wc = new WebClient();


var json = (JObject)JsonConvert.DeserializeObject(wc.DownloadString("http://services.runescape.com/m=website-data/playerDetails.ws?names=[%22" + Username.Replace(" ", "%20") + "%22]&callback=jQuery000000000000000_0000000000&_=0"));

the json its trying to deserialize...

jQuery000000000000000_0000000000([{"isSuffix":true,"recruiting":false,"name":"Sudo Bash","clan":"Linux Overlord","title":"the Troublesome"}]);

回答1:


In Json specification you can see that [ indicates the begining array of json objects while { indicates beginning of new json object.

Your json string starts with [ so it can contains more json objects ( because it's an array and it contains jQuery000000000000000_0000000000( which is your query string parameter. To get rid of the query string garbage you should find out the scheme of that garbage and then to process json object I would recommend you to deserialize your json string into List<JObject> using JsonConvert.DeserializeObject<T>() method if your json string starts with [ ( use standard type if it is starting with { );

Example :

string url = // url from @Darin Dimitrov answer
string response = wc.DownloadString(url);
// getting rid of the garbage 
response = response.Substring(response.IndexOf('(') + 1);
response = response.Substring(0, response.Length - 1);
// should get rid of "jQuery000000000000000_0000000000(" and last occurence of ")"
JObject result = null;
if(response.StartsWith('['))
{
    result = JsonConvert.DeserializeObject<List<JObject>>(response)[0];
}
else 
{
    result = JsonConvert.DeserializeObject<JObject>(response);
}



回答2:


What you are trying to deserialize is not JSON but rather JSONP (which is JSON wrapped in a function call).

Remove this parameter from your query string:

&callback=jQuery000000000000000_0000000000

and you should be good to go with a properly formatted JSON:

var url = "http://services.runescape.com/m=website-data/playerDetails.ws?names=[%22" + Username.Replace(" ", "%20") + "%22]&_=0";
var json = (JObject)JsonConvert.DeserializeObject(wc.DownloadString(url));


来源:https://stackoverflow.com/questions/41609214/unexpected-character-encountered-while-parsing-value-j-path-line-0-positi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!