本文介绍了RestSharp:从结果中获取null.调试结果时的数据althouh返回了JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用RestSharp并尝试反序列化JSON结果集,而当我运行代码时,尽管在调试时结果返回了JSON,但我却从result.Data
中获取了空值.
I am using RestSharp and trying to deserialize the JSON result set and when I run the code I'm getting null from the result.Data
although at the time of debugging the result has JSON returned.
private static void GetPanelList()
{
var client = new RestClient("http://CCCCCC.XXXXXX.com/API/v3/mailinglists/ML_dfgfghfghfh/contacts");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("x-api-token", "JHFKFKJFYILIOROIY");
// IRestResponse response = client.Execute(request);
var result = client.Execute<List<PanelList>>(request);
foreach(var i in result.Data)
{
foreach(var j in i.elements)
{
Console.WriteLine(j.firstName);
}
}
这是我的POCO:
public class PanelList
{
public List<elements> elements { get; set; }
}
public class elements
{
public string id { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string externalDataReference { get; set; }
public List<EmbededData> ED { get; set; }
public List<ResponseHistory> RH { get; set; }
public List<EmailHistory> EH { get; set; }
}
这是我试图解析的json结果的样子.
and here is how my json result look like that am trying to parse.
{ "result": {
"elements": [
{
"id": "MLRP_b7q690QRSqCxVuR",
"firstName": "S1-User1-firstname",
"lastName": "S1-U2-lastname",
"email": "[email protected]",
"externalDataReference": null,
"embeddedData": {
"DateTaken": "20160519",
"TriggerResponseID": "R_3fE6zgBzLa24dgD",
"TriggerSurveyID": "SV_3TXTMnJlsUxVGRL"
},
"language": null,
"unsubscribed": false,
"responseHistory": [
{
"responseId": "R_3fE6zgBzLa24dgD",
"surveyId": "SV_3TXTMnJlsUxVGRL",
"date": "2016-05-20T04:09:09Z",
"emailDistributionId": null,
"finishedSurvey": true
}
],
"emailHistory": [
{
"emailDistributionId": "EMD_41wVLKlQaADQBcF",
"date": "2016-05-24T00:33:02Z",
"type": "Invite",
"result": "Success",
"surveyId": "SV_8wFieltINfFL2gl",
"read": false
}
]
}]}}
推荐答案
您不是要读入结果容器"对象吗?
Shouldn't you be reading into a result "container" object?
容器类:
public class PanelListContainer
{
public PanelList result { get; set; }
}
获取代码:
var result = client.Execute<PanelListContainer>(request);
这篇关于RestSharp:从结果中获取null.调试结果时的数据althouh返回了JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!