如何在 C# 中使用 JSON 反序列化 oData V2?
How to deserialize oData V2 with JSON in C#?
我正在尝试使用 C# 反序列化 oData V2,但值始终为空:
{
"d": {
"results": [
{
"__metadata": {
"id": "http://vhcalnplci:8000/sap/opu/odata/SAP/ZEMPLOYEE_SRV/EmployeeEntitySet('111111')",
"uri": "http://vhcalnplci:8000/sap/opu/odata/SAP/ZEMPLOYEE_SRV/EmployeeEntitySet('111111')",
"type": "ZEMPLOYEE_SRV.EmployeeEntity"
},
"Empno": "111111",
"Fname": "Test Firstname",
"Lname": "Test Lastname",
"Addrs": "Test Address",
"Desgn": "Test Job"
}
]
}
}
我在 Visual Studio 中尝试了 CLR Class 生成,并创建了以下 类
public class Rootobject
{
public D d { get; set; }
}
public class D
{
public Result[] results { get; set; }
}
public class Result
{
public __Metadata __metadata { get; set; }
public string Empno { get; set; }
public string Fname { get; set; }
public string Lname { get; set; }
public string Addrs { get; set; }
public string Desgn { get; set; }
}
public class __Metadata
{
public string id { get; set; }
public string uri { get; set; }
public string type { get; set; }
}
internal class OData<Result>
{
public List<Result> Value { get; set; }
}
我用过的方法结果总是零
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(new Uri(url));
var json = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<OData<Employee>>(json);
var employee = result.Value;
}
你能告诉我我做错了什么吗?谢谢!
更改此行
var result = JsonConvert.DeserializeObject<OData<Employee>>(json);
进入
var result = JsonConvert.DeserializeObject<Rootobject>(json);
我正在尝试使用 C# 反序列化 oData V2,但值始终为空:
{
"d": {
"results": [
{
"__metadata": {
"id": "http://vhcalnplci:8000/sap/opu/odata/SAP/ZEMPLOYEE_SRV/EmployeeEntitySet('111111')",
"uri": "http://vhcalnplci:8000/sap/opu/odata/SAP/ZEMPLOYEE_SRV/EmployeeEntitySet('111111')",
"type": "ZEMPLOYEE_SRV.EmployeeEntity"
},
"Empno": "111111",
"Fname": "Test Firstname",
"Lname": "Test Lastname",
"Addrs": "Test Address",
"Desgn": "Test Job"
}
]
}
}
我在 Visual Studio 中尝试了 CLR Class 生成,并创建了以下 类
public class Rootobject
{
public D d { get; set; }
}
public class D
{
public Result[] results { get; set; }
}
public class Result
{
public __Metadata __metadata { get; set; }
public string Empno { get; set; }
public string Fname { get; set; }
public string Lname { get; set; }
public string Addrs { get; set; }
public string Desgn { get; set; }
}
public class __Metadata
{
public string id { get; set; }
public string uri { get; set; }
public string type { get; set; }
}
internal class OData<Result>
{
public List<Result> Value { get; set; }
}
我用过的方法结果总是零
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(new Uri(url));
var json = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<OData<Employee>>(json);
var employee = result.Value;
}
你能告诉我我做错了什么吗?谢谢!
更改此行
var result = JsonConvert.DeserializeObject<OData<Employee>>(json);
进入
var result = JsonConvert.DeserializeObject<Rootobject>(json);