2016-03-08 50 views
0

我打電話給一個REST服務來取回JSON結構。解串嵌套的JSON困難

我得到的錯誤是

Test method BarPanda.Web.Services.Test.PosServiceTests.GetMenu threw exception: 
System.MissingMethodException: No parameterless constructor defined for this object. 

原來的JSON如下(部分)

{ 
    "count": 3, 
    "limit": 50, 
    "_links": { 
    "self": { 
     "etag": "b49a27c7e7c663af8d6a736e24fac7f5", 
     "href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/" , 
     "profile": "https://panel.omnivore.io/docs/api#category_list"  
    } 
    }, 
    "_embedded": { 
    "categories": [ 
     { 
     "id": "AdiRjiAp", 
     "name": "Drinks", 
     "_links": { 
      "items": { 
      "etag": "05dad4d734401321a4854cf4f0369102", 
      "href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/AdiRjiAp/items/" , 
      "profile": "https://panel.omnivore.io/docs/api#menu-item_list"  
      }, 
      "self": { 
      "etag": "05dad4d734401321a4854cf4f0369102", 
      "href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/AdiRjiAp/" , 
      "profile": "https://panel.omnivore.io/docs/api#category_retrieve"  
      } 
     }, 
     "_embedded": { 
      "items": [ 
      { 
       "id": "gki84ia9", 
       "in_stock": true, 
       "modifier_groups_count": 0, 
       "name": "Soda", 
       "open": false, 
       "pos_id": "gki84ia9", 
       "price": 150, 
       "price_levels": [ 
       { 
        "id": "Byineidy", 
        "price": 150 
       }, 
       { 
        "id": "g4T4dTBj", 
        "price": 200 
       }, 
       { 
        "id": "K6czkc8b", 
        "price": 250 
       } 
       ], 
       "_links": { 
       "modifier_groups": { 
        "href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/gki84ia9/modifier_groups/" , 
        "profile": "https://panel.omnivore.io/docs/api#modifier-group_list"  
       }, 
       "self": { 
        "etag": "c59b380aed5c1f33915b028b739df955", 
        "href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/gki84ia9/" , 
        "profile": "https://panel.omnivore.io/docs/api#menu-item_retrieve"  
       } 
       } 
      }, 
      { 
       "id": "doTaLTyg", 
       "in_stock": true, 
       "modifier_groups_count": 0, 
       "name": "Orange Juice", 
       "open": false, 
       "pos_id": "doTaLTyg", 
       "price": 175, 
       "price_levels": [ 
       { 
        "id": "L4iqKid8", 
        "price": 175 
       }, 
       { 
        "id": "K6T8MTzb", 
        "price": 300 
       } 
       ], 
       "_links": { 
       "modifier_groups": { 
        "href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/doTaLTyg/modifier_groups/" , 
        "profile": "https://panel.omnivore.io/docs/api#modifier-group_list"  
       }, 
       "self": { 
        "etag": "d3ae9754edb321f18e192ebea446baeb", 
        "href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/doTaLTyg/" , 
        "profile": "https://panel.omnivore.io/docs/api#menu-item_retrieve"  
       } 
       } 
      } 
      ] 
     } 
     }, 

我想用下面的代碼和對象類反序列化

var response = _client.Execute(request); 
var converter = new JsonDeserializer(); 
      var menu = converter.Deserialize<PosMenu>(response); 

PosMenu

[DataContract] 
    public class PosMenu 
    { 
     [DataMember] 
     public int VenueId { get; set; } 

     [DataMember] 
     public int count { get; set; } 

     [DataMember] 
     public PosMenuEmbedded _embedded { get; set; } 
} 

PosMenuEmbedded

[DataContract] 
    public class PosMenuEmbedded 
    { 
     [DataMember] 
     public long UniqueId { get; set; } 

     [DataMember] 
     public PosMenuCategory[] categories { get; set; } 

     [DataMember] 
     public int PosMenuId { get; set; } 
} 

PosMenuCategory

[DataContract] 
    public class PosMenuCategory 
    { 
} 

注:我已採取了所有的屬性了這個類的,現在只是爲了看看,如果我能得到它的一個空白類的工作,但很可惜不。

如果我在PosMenuEmbedded

public PosMenuCategory[] categories { get; set; } 

註釋掉該行成功爲止。如果我把它放回去,即使是空課,也會失敗。

任何人都可以提出爲什麼這可能是?

+0

你可以發佈堆棧跟蹤嗎? – Idan

+0

你可以發佈'PosServiceTests.GetMenu'的定義嗎 – CarbineCoder

+0

@NZJames你是否在跳過解析數據時的_links? – Vinod

回答

1
[DataMember] 
    public List<PosMenuCategory> categories { get; set; }