- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我的小图书馆中,我正在编写一个副项目,我正在使用 RestSharp 从 Web API 获取 Json。模型类的反序列化适用于简单类型,但在某些端点,在请求时结果类型未知(或不清楚)。
特别是 GuildWars2 API v1,其中一个例子是元素数据。当然每个项目都有基本属性,并且根据查询的项目设置附加值。例如武器有一些修饰符等等。
我的想法是为所有基本属性创建一个抽象的 Item 类,并从该类派生用于添加所需属性的所有现有项目子类型。我的问题是,如何决定反序列化到哪个子类。我已经阅读了各种涉及向 Json 字符串添加限定类型名称的方法,但由于我仅从 API 获取数据,因此我无法影响我获取的响应。
理想情况下,我总是希望反序列化到基类 Item 中,反序列化器通过 Json 字符串中包含的给定属性决定实际类型。这有可能吗?或者有没有更好的方法来解决这个问题?
提前致谢
编辑:这是武器的 Json 示例:
{
"item_id": "30704",
"name": "Twilight",
"description": "",
"type": "Weapon",
"level": "80",
"rarity": "Legendary",
"vendor_value": "100000",
"icon_file_id": "456031",
"icon_file_signature": "CE3AF0B7B9BB6244726779F5B6A930541BA6C15F",
"game_types": ["Activity", "Dungeon", "Pve", "Wvw"],
"flags": ["HideSuffix", "NoSell", "SoulBindOnUse"],
"restrictions": [],
"weapon": {
"type": "Greatsword",
"damage_type": "Physical",
"min_power": "995",
"max_power": "1100",
"defense": "0",
"infusion_slots": [],
"infix_upgrade": {
"attributes": [
{
"attribute": "Power",
"modifier": "179"
},
{
"attribute": "Power",
"modifier": "179"
}
]
},
"suffix_item_id": "24599"
}
}
这将是一件盔甲:
{
"item_id":"500",
"name":"Carrion Conjurer Chest of Dwayna",
"description":"",
"type":"Armor",
"level":"68",
"rarity":"Rare",
"vendor_value":"257",
"icon_file_id":"61023",
"icon_file_signature":"76CD08463A05730071D400254141B50E570662D3",
"game_types":["Activity", "Dungeon", "Pve", "Wvw"],
"flags":["SoulBindOnUse"],
"restrictions":[],
"armor": {
"type":"Coat",
"weight_class":"Light",
"defense":"221",
"infusion_slots":[],
"infix_upgrade": {
"attributes": [
{
"attribute":"ConditionDamage","modifier":"70"
},
{
"attribute":"Power","modifier":"50"
}
]
},
"suffix_item_id":"24767"
}
}
我想根据属性“Type”的内容进行反序列化,但我不知道它是如何工作的 =/
最佳答案
您可以使用 JObject 解析 JSON 数据,然后将其用作关联数组。
string json = @"{
"item_id": "30704",
"name": "Twilight",
"description": "",
"type": "Weapon",
...
}";
JObject o = JObject.Parse(json);
Item item = null;
switch (o["type"])
{
case "Weapon":
item = JsonConvert.DeserializeObject<Weapon>(json);
break;
case "Armor":
item = JsonConvert.DeserializeObject<Armor>(json);
break;
default:
// throw error?
}
然后你就有了这样一个基类:
public class Item
{
public string item_id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string type { get; set; }
...
// List all common properties here
public Item() { }
// It's important to have the public default constructor implemented
// if you intend on overloading it for your own purpose later
}
你的武器类可以是这样的:
public class Weapon : Item // Weapon extends Item
{
public WeaponDetails weapon { get; set; }
public Weapon() { }
}
和武器细节:
public class WeaponDetails
{
public string type { get; set; }
public string damage_type { get; set; }
public int min_power { get; set; }
public int max_power { get; set; }
...
public WeaponDetails() { }
}
关于c# - 根据内容将 JSON 动态反序列化为派生类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18282784/
出于好奇,我尝试了一些原型(prototype)制作,但似乎只允许在第一个位置使用子例程的原型(prototype) &。 当我写作时 sub test (&$$) { do_somethin
我需要开发一个类似于 Android Play 商店应用程序或类似 this app 的应用程序.我阅读了很多教程,发现几乎每个教程都有与 this one 类似的例子。 . 我已经开始使用我的应用程
考虑一个表示“事件之间的时间”的列: (5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2) 我想将这些分组到 30 个桶中,但桶会重置。期望的结果: (0, 1,
我是一名优秀的程序员,十分优秀!