【问题标题】:How can I deserialize a JSON field that is named "default" in the JSON? C#如何反序列化 JSON 中名为“default”的 JSON 字段? C#
【发布时间】:2016-08-27 13:18:27
【问题描述】:

我正在使用 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# 中,我不能将参数命名为“默认”。虽然我无法更改 JSON,但我需要这个默认值。如果我将其命名为不同的名称,它不会反序列化; "medium" 和 "high" 正确反序列化。

【问题讨论】:

    标签: c# json serialization deserialization json-deserialization


    【解决方案1】:

    使用@ 转义关键字。

    public Thumbnail @default { get; set; }
    

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      相关资源
      最近更新 更多