难以反序列化嵌套的JSON

时间:2016-03-08 11:57:32

标签: c# json rest

我正在调用REST服务来获取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; }

成功了。如果我把它重新插入,它就会失败,即使是空课。

有谁能说明为什么会这样?

1 个答案:

答案 0 :(得分:1)

std::iostream