我仍然是新手,正在学习一些API调用。只是不知道为什么代码不能从字符串转换为Newtonsoft.Json.Linq.JObject。
WebRequest request = WebRequest.Create("https://api.pandascore.co/lol/champions?filter[name]=Brand&token==mytoken");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string responseFromServer = reader.ReadToEnd();
JObject parsedString = JObject.Parse(responseFromServer);
Champions champion = parsedString.ToObject<Champions>();
return View(champion);
在调试模式下,responseFromServer是一个字符串 结果看起来还可以,但这不会转换为对象。 parsedString = null。
Newtonsoft.Json.JsonReaderException:“从JsonReader读取JObject时出错。当前JsonReader项不是对象:StartArray。路径
Champions类如下:
public class Champions
{
public List<string> videogame_versions { get; set; }
public double spellblockperlevel { get; set; }
public double spellblock { get; set; }
public string name { get; set; }
public double mpregenperlevel { get; set; }
public double mpregen { get; set; }
public double mpperlevel { get; set; }
public double mp { get; set; }
public double movespeed { get; set; }
public string image_url { get; set; }
public int id { get; set; }
public double hpregenperlevel { get; set; }
public double hpregen { get; set; }
public double hpperlevel { get; set; }
public double hp { get; set; }
public double critperlevel { get; set; }
public double crit { get; set; }
public string big_image_url { get; set; }
public double attackspeedperlevel { get; set; }
public object attackspeedoffset { get; set; }
public double attackrange { get; set; }
public double attackdamageperlevel { get; set; }
public double attackdamage { get; set; }
public double armorperlevel { get; set; }
public double armor { get; set; }
}
}
我的JSON字符串如下:
[
{
"videogame_versions": [
"9.10.1",
"9.9.1",
"9.8.1",
"9.7.2",
"9.7.1",
"9.6.1",
"9.5.1",
"9.4.1",
"9.3.1",
"9.2.1",
"9.1.1",
"8.24.1",
"8.23.1",
"8.22.1"
],
"spellblockperlevel": 0.5,
"spellblock": 30,
"name": "Brand",
"mpregenperlevel": 0.6,
"mpregen": 10.665,
"mpperlevel": 21,
"mp": 469,
"movespeed": 340,
"image_url": "https://cdn.pandascore.co/images/lol/champion/image/7aa667709a7ce82e45c459e3df2d160a.png",
"id": 2347,
"hpregenperlevel": 0.55,
"hpregen": 5.5,
"hpperlevel": 88,
"hp": 519.68,
"critperlevel": 0,
"crit": 0,
"big_image_url": "https://cdn.pandascore.co/images/lol/champion/big_image/8ba7fd90e7c250b2dcc3183205ad6d94.jpg",
"attackspeedperlevel": 1.36,
"attackspeedoffset": null,
"attackrange": 550,
"attackdamageperlevel": 3,
"attackdamage": 57.04,
"armorperlevel": 3.5,
"armor": 21.88
}
]
提供6个王国和破碎的车轮
答案 0 :(得分:0)
这里的技巧是将deserialize
变成List<Champions>
,因为您的根级别JSON数据是一个数组。
public static class Program
{
private static void Main(string[] args)
{
string data = @"
[
{
'videogame_versions': [
'9.10.1',
'9.9.1',
'9.8.1',
'9.7.2',
'9.7.1',
'9.6.1',
'9.5.1',
'9.4.1',
'9.3.1',
'9.2.1',
'9.1.1',
'8.24.1',
'8.23.1',
'8.22.1'
],
'spellblockperlevel': 0.5,
'spellblock': 30,
'name': 'Brand',
'mpregenperlevel': 0.6,
'mpregen': 10.665,
'mpperlevel': 21,
'mp': 469,
'movespeed': 340,
'image_url': 'https://cdn.pandascore.co/images/lol/champion/image/7aa667709a7ce82e45c459e3df2d160a.png',
'id': 2347,
'hpregenperlevel': 0.55,
'hpregen': 5.5,
'hpperlevel': 88,
'hp': 519.68,
'critperlevel': 0,
'crit': 0,
'big_image_url': 'https://cdn.pandascore.co/images/lol/champion/big_image/8ba7fd90e7c250b2dcc3183205ad6d94.jpg',
'attackspeedperlevel': 1.36,
'attackspeedoffset': null,
'attackrange': 550,
'attackdamageperlevel': 3,
'attackdamage': 57.04,
'armorperlevel': 3.5,
'armor': 21.88
}
]
";
List<Champions> champions = JsonConvert.DeserializeObject<List<Champions>>(data);
}
public class Champions
{
public List<string> videogame_versions { get; set; }
public double spellblockperlevel { get; set; }
public double spellblock { get; set; }
public string name { get; set; }
public double mpregenperlevel { get; set; }
public double mpregen { get; set; }
public double mpperlevel { get; set; }
public double mp { get; set; }
public double movespeed { get; set; }
public string image_url { get; set; }
public int id { get; set; }
public double hpregenperlevel { get; set; }
public double hpregen { get; set; }
public double hpperlevel { get; set; }
public double hp { get; set; }
public double critperlevel { get; set; }
public double crit { get; set; }
public string big_image_url { get; set; }
public double attackspeedperlevel { get; set; }
public object attackspeedoffset { get; set; }
public double attackrange { get; set; }
public double attackdamageperlevel { get; set; }
public double attackdamage { get; set; }
public double armorperlevel { get; set; }
public double armor { get; set; }
}
}
答案 1 :(得分:0)
{ /*key value pairs*/ }
代表一个称为JArray
的JSON对象,它实际上应该是JToken
如果您不想指定为JSON对象或JSON数组,则可以改用 let url = URL(string: "image url here")
if url != nil {
DispatchQueue.global().async { [weak self] in
if let data = try? Data(contentsOf: url!) {
if let image = UIImage(data: data) {
DispatchQueue.main.async {
self.profileImage.image = image
}
}
}
}
}
。
答案 2 :(得分:0)
如果您的json位于数组中,则必须转换List对象
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var json = @"[
{
'videogame_versions': [
'9.10.1',
'9.9.1',
'9.8.1',
'9.7.2',
'9.7.1',
'9.6.1',
'9.5.1',
'9.4.1',
'9.3.1',
'9.2.1',
'9.1.1',
'8.24.1',
'8.23.1',
'8.22.1'
],
'spellblockperlevel': 0.5,
'spellblock': 30,
'name': 'Brand',
'mpregenperlevel': 0.6,
'mpregen': 10.665,
'mpperlevel': 21,
'mp': 469,
'movespeed': 340,
'image_url': 'https://cdn.pandascore.co/images/lol/champion/image/7aa667709a7ce82e45c459e3df2d160a.png',
'id': 2347,
'hpregenperlevel': 0.55,
'hpregen': 5.5,
'hpperlevel': 88,
'hp': 519.68,
'critperlevel': 0,
'crit': 0,
'big_image_url': 'https://cdn.pandascore.co/images/lol/champion/big_image/8ba7fd90e7c250b2dcc3183205ad6d94.jpg',
'attackspeedperlevel': 1.36,
'attackspeedoffset': null,
'attackrange': 550,
'attackdamageperlevel': 3,
'attackdamage': 57.04,
'armorperlevel': 3.5,
'armor': 21.88
}
]";
List<Champions> champions = JsonConvert.DeserializeObject<List<Champions>>(json);
}
}