我正在自動化一個API。我想反序列化我得到的JSON
響應,但我無法弄清楚如何去做。這是JSON
,我得到的迴應:反序列化複雜的JSON結果
{
"class": [
"Paged-Collection",
"Products"
],
"properties": {
"defaultPageSize": 10,
"skip": null,
"top": 10,
"totalRows": 2
},
"entities": [
{
"class": [
"Product"
],
"rel": [],
"properties": {
"Supplier": "Supplier1",
"Currency": "USD",
"ProductCode": "SomeCode1",
"price": 2
}
},
{
"class": [
"Product"
],
"rel": [],
"properties": {
"Supplier": "Supplier2",
"Currency": "USD",
"ProductCode": "SomeCode2",
"price": 73
}
},
],
"links": [
{
"rel": [
"self"
],
"href": "http://localhost...",
"method": "GET",
"title": null,
"type": null
}
]
}
從我在其他例子中所見到的反序列化歸結爲找出確切的數據strucutre(不知道但如果這是什麼),但是我做了幾個嘗試(不只是搶着SO的答案),所以這是我最後一次嘗試:
的結構,我看到它:
public class Data
{
List<string> @class { get; set; }
Dictionary<string, MetaData> properties { get; set; }
List<entities> entities { get; set; }
List<LinkFeed> links { get; set; }
}
public class MetaData
{
public int defaultPageSize { get; set; }
public string skip { get; set; }
public int top { get; set; }
public int totalRows { get; set; }
}
public class entities
{
public List<string> @class { get; set; }
public List<string> rel { get; set; }
public properties property { get; set; }
}
public class properties
{
public string Supplier { get; set; }
public string Currency { get; set; }
public string ProductCode { get; set; }
public string price { get; set; }
public double Price { get; set; }
}
public class LinkFeed
{
public List<string> rel { get; set; }
public string href { get; set; }
public string method { get; set; }
public string title { get; set; }
public string type { get; set; }
}
而且使用Newtonsoft.Json
實際的反序列化:
var result = JsonConvert.DeserializeObject<Data>(data);
一切是Null
..
其實是我真正感興趣的數據是,在"entities"
,但我不知道,如果只是提取會其實更容易或更困難。
作爲迴應你得到了什麼?一個json字符串,一個json文件或一個json對象? – LordNeo
對象但@CodingYoshi alreday想通了 – Leron