我正在使用的API会返回一些数据,这些数据正在努力解决如何构建将其反序列化为的类的情况。
这些年来,我变得很懒惰,在Visual Studio中,使用了Edit> Paste Special> Paste JSON作为类。
多年来,此选项为我提供了很好的服务,但可能导致我不熟练进行手动操作。
因此,我返回的数据在Fiddler中如下所示:
如您所见,有3个竞争对手取得了结果。我正在尝试获取这些数据。
当我执行我忠实的“将JSON复制为类”时,结果如下:
public class Competitor_Results
{
public _36629 _36629 { get; set; }
public _36628 _36628 { get; set; }
public _36627 _36627 { get; set; }
}
public class _36629
{
public string rank_type { get; set; }
public int? rank { get; set; }
public object position_places_image { get; set; }
public int? position_organic { get; set; }
public int? position_local_pack { get; set; }
public object position_knowledge_panel { get; set; }
public object position_featured_snippet { get; set; }
public object[] position_change_cache { get; set; }
public int last_week_change { get; set; }
public int last_month_change { get; set; }
public int last_day_change { get; set; }
}
public class _36628
{
public string rank_type { get; set; }
public int? rank { get; set; }
public object position_places_image { get; set; }
public int? position_organic { get; set; }
public int? position_local_pack { get; set; }
public object position_knowledge_panel { get; set; }
public object position_featured_snippet { get; set; }
public object[] position_change_cache { get; set; }
public int last_week_change { get; set; }
public int last_month_change { get; set; }
public int last_day_change { get; set; }
}
public class _36627
{
public string rank_type { get; set; }
public int? rank { get; set; }
public object position_places_image { get; set; }
public int? position_organic { get; set; }
public int? position_local_pack { get; set; }
public int? position_knowledge_panel { get; set; }
public object position_featured_snippet { get; set; }
public object[] position_change_cache { get; set; }
public int last_week_change { get; set; }
public int last_month_change { get; set; }
public int last_day_change { get; set; }
}
我无法弄清楚班级的样子,使我无法获得3个“竞争对手结果”,并且可以重复用于其他竞争对手的更多/更少结果。
有人可以给我一些指导吗?
这是与“竞争对手结果”相关的JSON
{
"36629": {
"rank_type": null,
"rank": null,
"position_places_image": null,
"position_organic": null,
"position_local_pack": null,
"position_knowledge_panel": null,
"position_featured_snippet": null,
"position_change_cache": [
null,
"2019-03-21T19:23:06.177931Z"
],
"last_week_change": 0,
"last_month_change": 0,
"last_day_change": 0
},
"36628": {
"rank_type": "local_pack",
"rank": 3,
"position_places_image": null,
"position_organic": 50,
"position_local_pack": 3,
"position_knowledge_panel": null,
"position_featured_snippet": null,
"position_change_cache": [
null,
"2019-03-21T19:23:06.178797Z"
],
"last_week_change": 0,
"last_month_change": 0,
"last_day_change": 0
},
"36627": {
"rank_type": "local_pack",
"rank": 2,
"position_places_image": null,
"position_organic": 43,
"position_local_pack": 2,
"position_knowledge_panel": null,
"position_featured_snippet": null,
"position_change_cache": [
null,
"2019-03-20T20:25:35.263147Z"
],
"last_week_change": 0,
"last_month_change": 0,
"last_day_change": 0
}
}
答案 0 :(得分:1)
我见过的大多数JSON类生成器都无法解决这种情况,即属性名称是动态的。生成代码后,您将拥有一堆表示项目的冗余类定义(例如_36628
,_36629
等)和一个容器类(例如Competitor_Results
),每个类都有引用一个。
以下是修复生成的代码的方法:
CompetitorResult
之类的明智名称。Dictionary<string, T>
替换容器类,其中T
是您的项目类的名称。换句话说,将引用容器类的属性的类型更改为Dictionary<string, T>
,然后删除容器类本身。您应该以如下形式结束:
public class RootObject
{
...
public Dictionary<string, CompetitorResult> competitor_results { get; set; }
...
}
public class CompetitorResult
{
public string rank_type { get; set; }
public int? rank { get; set; }
public object position_places_image { get; set; }
public int? position_organic { get; set; }
public int? position_local_pack { get; set; }
public object position_knowledge_panel { get; set; }
public object position_featured_snippet { get; set; }
public object[] position_change_cache { get; set; }
public int last_week_change { get; set; }
public int last_month_change { get; set; }
public int last_day_change { get; set; }
}