gpt4 book ai didi

c# - 将 json 反序列化为对象列表 - 无法创建和填充列表类型

转载 作者:太空宇宙 更新时间:2023-11-03 17:39:16 24 4
gpt4 key购买 nike

<分区>

我正在尝试将 json(这里是它的链接 http://antica.e-sim.org/apiFights.html?battleId=3139&roundId=1)反序列化为对象列表。

这是我的课:

public class RootObject
{
public int Damage { get; set; }
public int Weapon { get; set; }
public bool Berserk { get; set; }
public bool DefenderSide { get; set; }
public double MilitaryUnitBonus { get; set; }
public int Citizenship { get; set; }
public int CitizenId { get; set; }
public bool LocalizationBonus { get; set; }
public string Time { get; set; }
public int MilitaryUnit { get; set; }
}

public class Battles: IEnumerable<RootObject>
{
public IEnumerable<RootObject> battles { get; set; }
public IEnumerator<RootObject> GetEnumerator()
{
return battles.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return battles.GetEnumerator();
}
}

这就是我获取 json 并尝试反序列化它的方式:

string url = "http://" + serverEsim + ".e-sim.org/apiFights.html?battleId=" +
battles.ElementAt(k).Key + "&roundId=" + i;
WebRequest req = WebRequest.Create(url);
WebResponse resp = req.GetResponse();
StreamReader jsr = new StreamReader(resp.GetResponseStream());
Battles battle = JsonConvert.DeserializeObject<Battles>(jsr.ReadToEnd());

当我尝试 desereliaze 时,它​​给了我一个异常(exception):

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot create and populate list type Project.Battles. Path '', line 3, position 1.

我做错了什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com