2016-02-13 109 views
0

我是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); 

      } 

但是没有办法,我可以在媒体对象访问数据。请帮忙!

谢谢,

回答

0

我发现了这个问题。当在如下面的代码的对象寻址的JSON对象:

"media": { 
      "450x450": [ 
       { 
       "id": 26587, 
       "path": "media\/450x450\/19e09206ae752105101d2242935959ab4b074869.jpg", 
       "file_path": "media\/450x450\/19e09206ae752105101d2242935959ab4b074869.jpg", 
       "image_type": "packshot", 
       "last_updated_at": "2015-11-16T00:00:00+0000" 
       } 
      ], 
      "900x900": [ 
       { 
       "id": 26587, 
       "path": "media\/900x900\/f24fb9a2c308ff091adbc6c3f744850005962d6f.jpg", 
       "file_path": "media\/900x900\/f24fb9a2c308ff091adbc6c3f744850005962d6f.jpg", 
       "image_type": "packshot", 
       "last_updated_at": "2015-11-16T00:00:00+0000" 
       } 
      ] 
      } 

有含有2个阵列,一种类型的“450x450”和其它类型的“900x900的“媒体”对象,以便解决这一数据你必须处理正确的孩子。

//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) 
      { 

       MediProduct MyProduct = new MediProduct(); 
       MyProduct = JsonConvert.DeserializeObject<MediProduct>(result.ToString()); 
       if (null == result["description"]) 
       { 
        MyProduct.description = "/"; 
       } 

       if (null != result["media"]["450x450"]) 
       { 
        //to get the picture array. You have to address the correct path, in this case ["media"] "[450x450] 
        _450X450[] Foto450x450 = JsonConvert.DeserializeObject<_450X450[]>(result["media"]["450x450"].ToString()); 
        _900X900[] Foto900x900 = JsonConvert.DeserializeObject<_900X900[]>(result["media"]["900x900"].ToString()); 
} 

} 

由结果搜索[“媒体”] [“450x450”]子我能得到正确的阵列中的数据。 我还发现,下面的语句,在我的班级定义中阻止了这些数据:

[JSONObject的(MemberSerialization = MemberSerialization.OptIn)]

我发现这个说法某处在这个论坛。但它没有做我想做的事。我评论了这一点,我可以访问我的数据!

问候,