我正在嘗試使用 C# 反序列化 oData V2,但 Value 始終為空:
{
"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 Generation,并創建了以下類
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; }
}
我在 Result 始終為零的情況下使用的方法
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;
}
你能告訴我我做錯了什么嗎?謝謝!
uj5u.com熱心網友回復:
改變這一行
var result = JsonConvert.DeserializeObject<OData<Employee>>(json);
進入
var result = JsonConvert.DeserializeObject<Rootobject>(json);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/342855.html
