RestSharp JsonDeserializer 列表对象
RestSharp JsonDeserializer list object
我想使用 RestSharp 反序列化对 JSON 数组进行反序列化。
public class details
{
public string id { get; set; }
public string tran_id { get; set; }
public string tran_type { get; set; }
public string tran_status { get; set; }
public string expiry_date_time { get; set; }
public string number { get; set; }
}
我的JSON如下:
{
"details": [
{
"id": "ebca66079b44",
"tran_id": "c9b1bce025f5",
"tran_type": "A",
"tran_status": "B",
"expiry_date_time": "2018-11-26T06:33:04+00:00",
"number": "12345678ABC"
},
{
"id": "ebca66079b44",7c2445c8-a5ba-4ad2-a38e-3ea682c60edf",
"tran_id": "3ea682c60edf",
"tran_type": "A",
"tran_status": "B",
"expiry_date_time": "2018-11-26T06:26:28+00:00",
"number": "22345678ABC"
},
{
"id": "ebca66079b44",
"tran_id": "e40c45817985",
"tran_type": "A",
"tran_status": "B",
"expiry_date_time": "2018-11-26T06:26:06+00:00",
"number": "32345678ABC"
}
]
}
我的代码是:
IRestResponse response = client.Execute(request);
//Deserialize Json
return new JsonDeserializer().Deserialize<List<details>>(response);
我可以得到 'details',但不能得到对象内部的列表。
你需要用一个对象来包含你的JSON数组数据,因为最外层是对象而不是数组。
public class JsonModel
{
public List<Detail> details { get; set; }
}
public class Detail
{
public string id { get; set; }
public string tran_id { get; set; }
public string tran_type { get; set; }
public string tran_status { get; set; }
public string expiry_date_time { get; set; }
public string number { get; set; }
}
这样使用。
new JsonDeserializer().Deserialize<JsonModel>(response);
备注
有一个 json 数据可能会从 "ebca66079b44",7c2445c8-a5ba-4ad2-a38e-3ea682c60edf",
数据中抛出错误。
有两种方法可以轻松创建模型。
你可以在Visual Studio中使用Web Essentials,使用Edit > Paste special > paste JSON as a class,你可以更容易地知道两者之间的关系Json 和模型。
如果您不能使用 Web Essentials,您可以使用 https://app.quicktype.io/?l=csharp 在线 JSON 来建模 class.
你可以尝试使用那些模型来携带你的JSON格式。
尝试Response.Data
这里是我的示例代码
public T Execute<T>(RestRequest request, HttpStatusCode expectedResponseCode) where T : new()
{
// Won't throw exception.
var response = _client.Execute<T>(request);
return response.Data;
}
public List<Project> GetProjects()
{
var request = new RestRequest()
{
Resource = ResourceUrls.Project(),
RequestFormat = DataFormat.Json,
Method = Method.GET
};
return Execute<List<Project>>(request, HttpStatusCode.OK);
}
我想使用 RestSharp 反序列化对 JSON 数组进行反序列化。
public class details
{
public string id { get; set; }
public string tran_id { get; set; }
public string tran_type { get; set; }
public string tran_status { get; set; }
public string expiry_date_time { get; set; }
public string number { get; set; }
}
我的JSON如下:
{
"details": [
{
"id": "ebca66079b44",
"tran_id": "c9b1bce025f5",
"tran_type": "A",
"tran_status": "B",
"expiry_date_time": "2018-11-26T06:33:04+00:00",
"number": "12345678ABC"
},
{
"id": "ebca66079b44",7c2445c8-a5ba-4ad2-a38e-3ea682c60edf",
"tran_id": "3ea682c60edf",
"tran_type": "A",
"tran_status": "B",
"expiry_date_time": "2018-11-26T06:26:28+00:00",
"number": "22345678ABC"
},
{
"id": "ebca66079b44",
"tran_id": "e40c45817985",
"tran_type": "A",
"tran_status": "B",
"expiry_date_time": "2018-11-26T06:26:06+00:00",
"number": "32345678ABC"
}
]
}
我的代码是:
IRestResponse response = client.Execute(request);
//Deserialize Json
return new JsonDeserializer().Deserialize<List<details>>(response);
我可以得到 'details',但不能得到对象内部的列表。
你需要用一个对象来包含你的JSON数组数据,因为最外层是对象而不是数组。
public class JsonModel
{
public List<Detail> details { get; set; }
}
public class Detail
{
public string id { get; set; }
public string tran_id { get; set; }
public string tran_type { get; set; }
public string tran_status { get; set; }
public string expiry_date_time { get; set; }
public string number { get; set; }
}
这样使用。
new JsonDeserializer().Deserialize<JsonModel>(response);
备注
有一个 json 数据可能会从 "ebca66079b44",7c2445c8-a5ba-4ad2-a38e-3ea682c60edf",
数据中抛出错误。
有两种方法可以轻松创建模型。
你可以在Visual Studio中使用Web Essentials,使用Edit > Paste special > paste JSON as a class,你可以更容易地知道两者之间的关系Json 和模型。
如果您不能使用 Web Essentials,您可以使用 https://app.quicktype.io/?l=csharp 在线 JSON 来建模 class.
你可以尝试使用那些模型来携带你的JSON格式。
尝试Response.Data
这里是我的示例代码
public T Execute<T>(RestRequest request, HttpStatusCode expectedResponseCode) where T : new()
{
// Won't throw exception.
var response = _client.Execute<T>(request);
return response.Data;
}
public List<Project> GetProjects()
{
var request = new RestRequest()
{
Resource = ResourceUrls.Project(),
RequestFormat = DataFormat.Json,
Method = Method.GET
};
return Execute<List<Project>>(request, HttpStatusCode.OK);
}