【发布时间】:2023-03-12 16:03:01
【问题描述】:
(帖子底部的完整错误消息)
我知道这已经在其他几篇文章中讨论过,但是,这些文章中提供的修复似乎对我不起作用。
我在下面提供我的所有代码。反序列化在方法parseSpellData()中。
示例 JSON:
{
"_id": "58c9eb75c9e7ce9f7214efaa",
"index": 1,
"name": "Acid Arrow",
"desc": [
"A shimmering green arrow streaks toward a target within range and bursts in a spray of acid. Make a ranged spell attack against the target. On a hit, the target takes 4d4 acid damage immediately and 2d4 acid damage at the end of its next turn. On a miss, the arrow splashes the target with acid for half as much of the initial damage and no damage at the end of its next turn."
],
"higher_level": [
"When you cast this spell using a spell slot of 3rd level or higher, the damage (both initial and later) increases by 1d4 for each slot level above 2nd."
],
"page": "phb 259",
"range": "90 feet",
"components": [
"V",
"S",
"M"
],
"material": "Powdered rhubarb leaf and an adder’s stomach.",
"ritual": "no",
"duration": "Instantaneous",
"concentration": "no",
"casting_time": "1 action",
"level": 2,
"school": {
"url": "http://dnd5eapi.co/api/magic-schools/5",
"name": "Evocation"
},
"classes": [
{
"name": "Wizard",
"url": "http://dnd5eapi.co/api/classes/12"
}
],
"subclasses": [
{
"url": "http://dnd5eapi.co/api/subclasses/2",
"name": "Lore"
},
{
"url": "http://dnd5eapi.co/api/subclasses/4",
"name": "Land"
}
],
"url": "http://dnd5eapi.co/api/spells/1"
}
代码:
class MainPageViewModel
{
public ObservableCollection<Spell> availableSpells { get; set; } = new ObservableCollection<Spell>();
public MainPageViewModel()
{
availableSpells = parseSpellData(getSpells());
}
public string getSpells()
{
string spells = string.Empty;
try
{
HttpWebRequest request = WebRequest.Create("http://dnd5eapi.co/api/Spells") as HttpWebRequest;
using (var response = request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
spells = reader.ReadToEnd();
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine(spells);
return spells;
}
public ObservableCollection<Spell> parseSpellData(string listOfSpells)
{
ObservableCollection<Spell> parsedSpellData = JsonConvert.DeserializeObject<ObservableCollection<Spell>>(listOfSpells);
return parsedSpellData as ObservableCollection<Spell>;
}
}
public class School
{
public string url { get; set; }
public string name { get; set; }
}
public class Class
{
public string name { get; set; }
public string url { get; set; }
}
public class Subclass
{
public string url { get; set; }
public string name { get; set; }
}
public class Spell
{
public string _id { get; set; }
public int index { get; set; }
public string name { get; set; }
public List<string> desc { get; set; }
public List<string> higher_level { get; set; }
public string page { get; set; }
public string range { get; set; }
public List<string> components { get; set; }
public string material { get; set; }
public string ritual { get; set; }
public string duration { get; set; }
public string concentration { get; set; }
public string casting_time { get; set; }
public int level { get; set; }
public School school { get; set; }
public List<Class> classes { get; set; }
public List<Subclass> subclasses { get; set; }
public string url { get; set; }
}
PM 可视化错误
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.ObjectModel.ObservableCollection´1[NInterpret.interpretedObject]' 因为该类型需要 JSON 数组(例如 [1 ,2,3]) 以正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是整数等原始类型,而不是集合类型可以从 JSON 对象反序列化的数组或列表。
【问题讨论】:
-
你发布的 JSON 不是数组,这是 observable 集合需要的。我不确定问题是什么?如果您真的想使用可观察的集合,您应该将其反序列化为单个项目并将其添加到集合中。