如何反序列化JSON中名为“default”的JSON字段? C#

时间:2016-05-02 17:26:48

标签: c# json serialization deserialization json-deserialization

我正在使用JavaScriptSerializer.Deserialize从youtubeapi反序列化JSON。我创建了类来实现JSON的数据结构,如下所示:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"kiOs9cZLH2FUp6r6KJ8eyq_LIOk/9dlz1IIYCkI6XFpNy28ZfHloSKY\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"kiOs9cZLH2FUp6r6KJ8eyq_LIOk/xNIcXxr4UjrZ1ZnL0lGaAAqLc4k\"",
   "id": "r8Ni2m3ro30",
   "snippet": {
    "publishedAt": "2013-09-25T17:49:08.000Z",
    "channelId": "UCeq7mOAcfee86UYyG8SCgvw",
    "title": "Long-Lasting Residual with Capreno Corn Herbicide",
    "description": "Watch growers talk about their experience with Capreno® corn herbicide's long-lasting residual. During unforgiving weather conditions, Capreno postemergence herbicide delivers season-long control of the toughest weeds to deliver an amazing end-of-season clean.",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/r8Ni2m3ro30/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/r8Ni2m3ro30/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/r8Ni2m3ro30/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    }
   }
  }
 ]
}

唯一的问题是缩略图。我做了以下课程:

public class Thumbnails
{
    public Thumbnail default { get; set; }
    public Thumbnail medium { get;set; }
    public Thumbnail high { get;set; }
}

public class Thumbnail
{
    public string url { get; set; }
    public int width { get;set; }
    public int height { get; set; }
}

但在C#中我不能将参数命名为“default”。我不能改变JSON,我需要这个默认值。如果我将其命名为不同的东西,它不会反序列化; “中”和“高”正确反序列化。

1 个答案:

答案 0 :(得分:5)

使用@来转义关键字。

public Thumbnail @default { get; set; }