JSON 反序列化对象 returns 空
JSON DeserializeObject returns null
我有一个 JSON 格式的响应:
{
"list": {
"historySteps": [
{
"actualUserDisplayName": "John Doe",
"actualUserID": "SiteOwner",
"invokerType": "USER",
"step": 10,
"timestampFormatted": "09/23/2019 12:37:04 PM",
"transitionName": "Work",
"userDisplayName": "John Doe",
"userID": "SiteOwner"
},
{
"actualUserDisplayName": "John Doe",
"actualUserID": "SiteOwner",
"comments": "Zebra",
"invokerType": "USER",
"step": 9,
"timestampFormatted": "09/23/2019 11:12:30 AM",
"transitionName": "Return",
"userDisplayName": "John Doe",
"userID": "SiteOwner"
},
{
"actualUserDisplayName": "John Doe",
"actualUserID": "SiteOwner",
"invokerType": "USER",
"step": 1,
"timestampFormatted": "09/23/2019 10:11:48 AM",
"transitionName": "Initiate",
"userDisplayName": "John Doe",
"userID": "SiteOwner"
}
]
}
}
我的 C# 代码为:
public historyList GetWorkFlowHistory(string workspaceId, string dmsId)
{
try {
var client = new RestClient(TenantUrl + $"/api/rest/v1/workspaces/{workspaceId}/items/{dmsId}/workflows/history");
client.Proxy = Proxy;
var request = new RestRequest(Method.GET);
request.AddCookie("customer", CustomerToken);
request.AddCookie("JSESSIONID", SessionId);
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);
if (response.ErrorException != null)
throw response.ErrorException;
else if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(response.Content);
var results = SimpleJson.SimpleJson.DeserializeObject<historyList>(response.Content);
return results;
}
catch (Exception ex)
{
Log.WriteLog("Exception in GetWorkFlowHistory() - " + ex.Message);
}
return null;
}
[XmlRoot(ElementName = "historySteps")]
public class HistorySteps
{
[XmlElement(ElementName = "actualUserDisplayName")]
public string ActualUserDisplayName { get; set; }
[XmlElement(ElementName = "actualUserID")]
public string ActualUserID { get; set; }
[XmlElement(ElementName = "comments")]
public string Comments { get; set; }
[XmlElement(ElementName = "invokerType")]
public string InvokerType { get; set; }
[XmlElement(ElementName = "step")]
public string Step { get; set; }
[XmlElement(ElementName = "timestampFormatted")]
public string TimestampFormatted { get; set; }
[XmlElement(ElementName = "transitionName")]
public string TransitionName { get; set; }
[XmlElement(ElementName = "userDisplayName")]
public string UserDisplayName { get; set; }
[XmlElement(ElementName = "userID")]
public string UserID { get; set; }
}
[XmlRoot(ElementName = "list")]
public class historyList
{
[XmlElement(ElementName = "historySteps")]
public HistorySteps HistorySteps { get; set; }
}
代码一直运行到 response.Content 点,它检索了内容。当我尝试反序列化对象时,我无法弄清楚我做错了什么。它 returns historySteps = null。我错过了什么?我采取的方法正确吗?
属性 public HistorySteps HistorySteps { get; set; }
不是 public List<HistorySteps> HistorySteps { get; set; }
的列表更改。
因为 SimpleJson 非常...简单,您必须将您的 属性 命名为 json,并且您将需要一个包装器 class "list"
json .
public class HistorySteps
{
public string actualUserDisplayName { get; set; }
public string actualUserID { get; set; }
public string comments { get; set; }
public string invokerType { get; set; }
public string step { get; set; }
public string timestampFormatted { get; set; }
public string transitionName { get; set; }
public string userDisplayName { get; set; }
public string userID { get; set; }
}
public class historyList
{
public List<HistorySteps> historySteps { get; set; }
}
public class MyResponse
{
public historyList list { get; set; }
}
首先,你处理的是json,而不是xml。您应该使用 DataContract/DataMember 或 JsonProperty.
注释您的属性
其次,你快完成了,你只需要一个额外的 class 来包装整个东西。 "list"是属性里面的根对象
我有一个 JSON 格式的响应:
{
"list": {
"historySteps": [
{
"actualUserDisplayName": "John Doe",
"actualUserID": "SiteOwner",
"invokerType": "USER",
"step": 10,
"timestampFormatted": "09/23/2019 12:37:04 PM",
"transitionName": "Work",
"userDisplayName": "John Doe",
"userID": "SiteOwner"
},
{
"actualUserDisplayName": "John Doe",
"actualUserID": "SiteOwner",
"comments": "Zebra",
"invokerType": "USER",
"step": 9,
"timestampFormatted": "09/23/2019 11:12:30 AM",
"transitionName": "Return",
"userDisplayName": "John Doe",
"userID": "SiteOwner"
},
{
"actualUserDisplayName": "John Doe",
"actualUserID": "SiteOwner",
"invokerType": "USER",
"step": 1,
"timestampFormatted": "09/23/2019 10:11:48 AM",
"transitionName": "Initiate",
"userDisplayName": "John Doe",
"userID": "SiteOwner"
}
]
}
}
我的 C# 代码为:
public historyList GetWorkFlowHistory(string workspaceId, string dmsId)
{
try {
var client = new RestClient(TenantUrl + $"/api/rest/v1/workspaces/{workspaceId}/items/{dmsId}/workflows/history");
client.Proxy = Proxy;
var request = new RestRequest(Method.GET);
request.AddCookie("customer", CustomerToken);
request.AddCookie("JSESSIONID", SessionId);
request.AddHeader("cache-control", "no-cache");
IRestResponse response = client.Execute(request);
if (response.ErrorException != null)
throw response.ErrorException;
else if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(response.Content);
var results = SimpleJson.SimpleJson.DeserializeObject<historyList>(response.Content);
return results;
}
catch (Exception ex)
{
Log.WriteLog("Exception in GetWorkFlowHistory() - " + ex.Message);
}
return null;
}
[XmlRoot(ElementName = "historySteps")]
public class HistorySteps
{
[XmlElement(ElementName = "actualUserDisplayName")]
public string ActualUserDisplayName { get; set; }
[XmlElement(ElementName = "actualUserID")]
public string ActualUserID { get; set; }
[XmlElement(ElementName = "comments")]
public string Comments { get; set; }
[XmlElement(ElementName = "invokerType")]
public string InvokerType { get; set; }
[XmlElement(ElementName = "step")]
public string Step { get; set; }
[XmlElement(ElementName = "timestampFormatted")]
public string TimestampFormatted { get; set; }
[XmlElement(ElementName = "transitionName")]
public string TransitionName { get; set; }
[XmlElement(ElementName = "userDisplayName")]
public string UserDisplayName { get; set; }
[XmlElement(ElementName = "userID")]
public string UserID { get; set; }
}
[XmlRoot(ElementName = "list")]
public class historyList
{
[XmlElement(ElementName = "historySteps")]
public HistorySteps HistorySteps { get; set; }
}
代码一直运行到 response.Content 点,它检索了内容。当我尝试反序列化对象时,我无法弄清楚我做错了什么。它 returns historySteps = null。我错过了什么?我采取的方法正确吗?
属性 public HistorySteps HistorySteps { get; set; }
不是 public List<HistorySteps> HistorySteps { get; set; }
的列表更改。
因为 SimpleJson 非常...简单,您必须将您的 属性 命名为 json,并且您将需要一个包装器 class "list"
json .
public class HistorySteps
{
public string actualUserDisplayName { get; set; }
public string actualUserID { get; set; }
public string comments { get; set; }
public string invokerType { get; set; }
public string step { get; set; }
public string timestampFormatted { get; set; }
public string transitionName { get; set; }
public string userDisplayName { get; set; }
public string userID { get; set; }
}
public class historyList
{
public List<HistorySteps> historySteps { get; set; }
}
public class MyResponse
{
public historyList list { get; set; }
}
首先,你处理的是json,而不是xml。您应该使用 DataContract/DataMember 或 JsonProperty.
注释您的属性其次,你快完成了,你只需要一个额外的 class 来包装整个东西。 "list"是属性里面的根对象