我是JSON的新手,難以從API反序列化JSON供稿。我使用Newtonsoft的JSON.NET。下面的JSON也顯示了包含媒體的產品。我可以訪問產品信息,id,cnk,名稱等......沒有問題。但問題在於媒體結構。媒體包含名稱爲「450x450」和「900x900」的陣列。每種類型都可以有一個,多個對象甚至沒有。將多級JSON反序列化到C#類的對象數組
的JSON提要:
`
{
"products" : [
{
"id": 44649,
"cnk": "2753982",
"language": "nl",
"last_updated_at": "2015-12-10T15:19:51+0000",
"status": "active",
"name": "EUCERIN tube",
"febelco_name": "EUCERIN tube 2",
"ean": "4005000019875",
"apb_category": "cosmetics",
"weight": 62,
"width": 38,
"height": 126,
"depth": 49,
"prescription": false,
"tax": 21,
"public_price_apb": 7.8,
"public_price_febelco": 12.9,
"media": {
"450x450": [
{
"id": 26587,
"path": "media\/450x450\/19e09206ae752105101d2235935959ab4b085869.jpg",
"file_path": "media\/450x450\/19e09206ae752105101d2235935959ab4b085869.jpg",
"image_type": "packshot",
"last_updated_at": "2015-11-16T00:00:00+0000"
}
],
"900x900": [
{
"id": 26587,
"path": "media\/900x900\/f24fb9a2c308ff091addf6c3f744850004962d6f.jpg",
"file_path": "media\/900x900\/f24fb9a2c308ff091addf6c3f744850004962d6f.jpg",
"image_type": "packshot",
"last_updated_at": "2015-11-16T00:00:00+0000"
}
]
}
}
]
}
`
我使用VS2013和我使用的自定義類的創作者功能:
EDIT -> Paste Special -> Paste JSON as Classes
這給以下課程:
public class MediProduct
{
public int id { get; set; }
public string cnk { get; set; }
public string language { get; set; }
public DateTime last_updated_at { get; set; }
public string status { get; set; }
public string name { get; set; }
public string ean { get; set; }
public string apb_category { get; set; }
public bool prescription { get; set; }
public int tax { get; set; }
public float public_price_apb { get; set; }
public float public_price_febelco { get; set; }
public Consumer_Categories[] consumer_categories { get; set; }
public Media media { get; set; }
public string febelco_name { get; set; }
public int weight { get; set; }
public int width { get; set; }
public int height { get; set; }
public int depth { get; set; }
public string description { get; set; }
}
public class Media
{
[JsonProperty(PropertyName = "450x450")]
public _450X450[] _450x450 { get; set; }
public _900X900[] _900x900 { get; set; }
}
public class _450X450
{
public int id { get; set; }
public string path { get; set; }
public string file_path { get; set; }
public string image_type { get; set; }
public DateTime last_updated_at { get; set; }
}
public class _900X900
{
public int id { get; set; }
public string path { get; set; }
public string file_path { get; set; }
public string image_type { get; set; }
public DateTime last_updated_at { get; set; }
}
所以我必須得到「媒體」項目,我可以有多個包含我想要的數據的「450x450」對象。我不知道如何達到這個水平,特別是我如何擺脫object reference is not set to an instance of an object
錯誤。我嘗試了以下幾件事:
`
//Get JSON result objects into a list
IList<JToken> results = PartProduct["products"].Children().ToList();
//serialize JSON results into .net objects
//IList<UsefulProduct> ReturnProducts = new List<UsefulProduct>();
IList<MediProduct> ReturnProducts = new List<MediProduct>();
//add the different products to the list
foreach (JToken result in results)
{
//UsefulProduct usefulProduct = JsonConvert.DeserializeObject<UsefulProduct>(result.ToString());
MediProduct MyProduct = JsonConvert.DeserializeObject<MediProduct>(result.ToString());
if (null == result["description"])
{
MyProduct.description = "/";
}
dynamic Rommel = JsonConvert.DeserializeObject<_450X450[]>(result["450x450"].ToString());
//Console.WriteLine(StommeFoto._450x450.id);
ReturnProducts.Add(MyProduct);
}
但是沒有辦法,我可以在媒體對象訪問數據。請幫忙!
謝謝,