我有以下json:
{
"13377": {
"id": 13377,
"orderId": 13377,
"executionStatus": "-1",
"comment": "",
"htmlComment": "",
"cycleId": -1,
"cycleName": "Ad hoc",
"versionId": 10001,
"versionName": "Version2",
"projectId": 10000,
"createdBy": "vm_admin",
"modifiedBy": "vm_admin",
"assignedTo": "user1",
"assignedToDisplay": "user1",
"assignedToUserName": "user1",
"assigneeType": "assignee",
"issueId": 10013,
"issueKey": "SAM-14",
"summary": "Test",
"label": "",
"component": "",
"projectKey": "SAM",
"folderId": 233,
"folderName": "testfolder"
}
}
我使用 json2csharp 创建以下类并获取:
public class __invalid_type__13377
{
public int id { get; set; }
public int orderId { get; set; }
public string executionStatus { get; set; }
public string comment { get; set; }
public string htmlComment { get; set; }
public int cycleId { get; set; }
public string cycleName { get; set; }
public int versionId { get; set; }
public string versionName { get; set; }
public int projectId { get; set; }
public string createdBy { get; set; }
public string modifiedBy { get; set; }
public string assignedTo { get; set; }
public string assignedToDisplay { get; set; }
public string assignedToUserName { get; set; }
public string assigneeType { get; set; }
public int issueId { get; set; }
public string issueKey { get; set; }
public string summary { get; set; }
public string label { get; set; }
public string component { get; set; }
public string projectKey { get; set; }
public int folderId { get; set; }
public string folderName { get; set; }
}
public class RootObject
{
public __invalid_type__13377 __invalid_name__13377 { get; set; }
}
当我在 C# 中反序列化时,我没有收到任何错误,但我得到 null?
不知道如何处理这个问题..欢迎任何建议。
谢谢。
可以使用字典反序列化 Json 对象。在这种情况下,您可以像现在一样构建数据对象:
public class Foo
{
public int id { get; set; }
public int orderId { get; set; }
...
}
并反序列化为
Dictionary<string, Foo>
如果您使用的是 Newtonsoft:
var data = JsonConvert.DeserializeObject<Dictionary<string,Foo>(json);
有时,当对象类型未知时,可以使用 dynamic 关键字:
var data = JsonConvert.DeserializeObject<dynamic>(json);