API 响应的 JSON 如下 连接 http://121.36.111.13:8021/getList2?pageNum=1&pageSize=10000&workOrderId=20 应该如何书写匹配的C#类
时间: 2024-10-06 17:06:51 浏览: 27
s:iterator 用法 (2).pdf
给定的API响应JSON数据通常需要映射到一个C#类结构中以便解析。假设响应的数据如下:
```json
{
"code": 200,
"message": "成功",
"data": {
"list": [
{"id": 1, "name": "Item 1", "description": "Description 1"},
{"id": 2, "name": "Item 2", "description": "Description 2"},
// 更多项...
],
"total": 10,
"pageInfo": {
"currentPage": 1,
"pageSize": 10,
"totalPage": 2
}
}
}
```
你可以创建一个名为`ApiResponse`的类,以及相关的嵌套类来表示数据结构:
```csharp
public class PageInfo
{
public int CurrentPage { get; set; }
public int PageSize { get; set; }
public int TotalPage { get; set; }
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class ResponseData
{
public List<Item> List { get; set; }
public PageInfo PageInfo { get; set; }
}
public class ApiResponse
{
public int Code { get; set; }
public string Message { get; set; }
public ResponseData Data { get; set; }
}
```
然后,你可以使用Json.NET库(如`Newtonsoft.Json`)来反序列化JSON到`ApiResponse`对象:
```csharp
var jsonString = await response.Content.ReadAsStringAsync();
ApiResponse apiResponse = JsonConvert.DeserializeObject<ApiResponse>(jsonString);
```
阅读全文