解析值时遇到意外字符:j.路径'',第0行,位置0 [英] Unexpected character encountered while parsing value: j. Path '', line 0, position 0
问题描述
t无法反序列化/处理此json,我尝试使用不同的方法进行多种组合以尝试使此工作有效,但似乎无济于事...
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...
我正在使用的代码
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"));
json试图反序列化...
the json its trying to deserialize...
jQuery000000000000000_0000000000([{"isSuffix":true,"recruiting":false,"name":"Sudo Bash","clan":"Linux Overlord","title":"the Troublesome"}]);
推荐答案
在Json规范中,您可以看到[
表示json对象的开始数组,而{
表示新json对象的开始.
In Json specification you can see that [
indicates the begining array of json objects while {
indicates beginning of new json object.
您的json字符串以[
开头,因此它可以包含更多json对象(因为它是一个数组,并且包含jQuery000000000000000_0000000000(
这是您的查询字符串参数.要摆脱查询字符串垃圾,您应该找出方案如果您的json字符串以[
开头,则建议您使用JsonConvert.DeserializeObject<T>()
方法将json字符串反序列化为List<JObject>
(如果以{
开头,请使用标准类型);
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 {
);
示例:
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);
}
这篇关于解析值时遇到意外字符:j.路径'',第0行,位置0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!