【问题标题】:Deserializing JSON Array to Object in C#在 C# 中将 JSON 数组反序列化为对象
【发布时间】:2018-02-04 09:00:24
【问题描述】:

我对 C# 比较陌生(以前使用 HTML/JS/Angular 的经验),我在反序列化从我正在使用的 API 中返回的 JSON 时遇到问题。

{  
   "titles":[  
  {  
     "lastUnlock":"2016-12-28T16:34:36.0390000Z",
     "titleId":566278,
     "serviceConfigId":"6ee10100-671e-4fc4-8cf1-91700008a406",
     "titleType":"DGame",
     "platform":"Durango",
     "name":"Game1",
     "earnedAchievements":4,
     "currentGamerscore":60,
     "maxGamerscore":1000
  },
  {  
     "lastUnlock":"2016-08-05T13:02:18.4140000Z",
     "titleId":10027721,
     "serviceConfigId":"28dd0100-1521-414e-a1d8-f0ba009902c9",
     "titleType":"DGame",
     "platform":"Durango",
     "name":"Game2",
     "earnedAchievements":17,
     "currentGamerscore":1000,
     "maxGamerscore":1000
  },
  {  
     "lastUnlock":"2016-05-02T20:52:40.3705214Z",
     "titleId":62572131,
     "serviceConfigId":"54240100-7870-4a47-8cec-7cfd03bac663",
     "titleType":"DGame",
     "platform":"Durango",
     "name":"Game3",
     "earnedAchievements":35,
     "currentGamerscore":1000,
     "maxGamerscore":1000
  },
     ],
   "pagingInfo":{  
      "continuationToken":null,
      "totalRecords":86
   }
}

问题是我不确定如何将其反序列化为对象数组。

我已经创建了一个对象类:

public class Game
    {
        public string name { get; set; }
        public string gamertag { get; set; }
        public string platform { get; set; }
        public int earnedAchievements { get; set; }
        public string currentGamerscore { get; set; }
        public string maxGamerscore { get; set; }
        public string lastUnlock { get; set; }
    }

从那里我尝试使用 JsonConvert.DeserializeObject(result) 但这只是返回不可用的“CompleteAdmin.Controllers.AchievementsAPIController+Game”。

谁能告诉我这应该如何设置?最终,我的目标是将其放入数据库。 :)

谢谢。

【问题讨论】:

  • 据我所知,这几乎是我当前的设置:games = JsonConvert.DeserializeObject(result);
  • 如果您的 JSON 是一个集合,那么它将是 JsonConvert.DeserializeObject> 或任何集合。
  • 请注意,在尝试反序列化您的 JSON 时,我收到一条错误消息,提示不应该存在尾随逗号。这是从pagingInfo倒数的第二个。

标签: c# html json api serialization


【解决方案1】:

很简单

在 Visual Studio 中右键单击解决方案并选择“管理 NuGet 包”,将在顶部搜索类型“newtonsoft”中打开一个菜单,选择带有黑色图标的第一个选项。并添加到您的项目中。然后写下。

public class Games
{
    public Game[] titles { get; set; }
}

public class Game
{


    public string name { get; set; }
    public string gamertag { get; set; }
    public string platform { get; set; }
    public int earnedAchievements { get; set; }
    public string currentGamerscore { get; set; }
    public string maxGamerscore { get; set; }
    public string lastUnlock { get; set; }
}

在页面加载或您想要结果的地方:

 string jsonObject = @"{  
                                   'titles':[  
                                  {  
                                     'lastUnlock':'2016-12-28T16:34:36.0390000Z',
                                     'titleId':566278,
                                     'serviceConfigId':'6ee10100-671e-4fc4-8cf1-91700008a406',
                                     'titleType':'DGame',
                                     'platform':'Durango',
                                     'name':'Game1',
                                     'earnedAchievements':4,
                                     'currentGamerscore':60,
                                     'maxGamerscore':1000
                                  },
                                  {  
                                     'lastUnlock':'2016-08-05T13:02:18.4140000Z',
                                     'titleId':10027721,
                                     'serviceConfigId':'28dd0100-1521-414e-a1d8-f0ba009902c9',
                                     'titleType':'DGame',
                                     'platform':'Durango',
                                     'name':'Game2',
                                     'earnedAchievements':17,
                                     'currentGamerscore':1000,
                                     'maxGamerscore':1000
                                  },
                                  {  
                                     'lastUnlock':'2016-05-02T20:52:40.3705214Z',
                                     'titleId':62572131,
                                     'serviceConfigId':'54240100-7870-4a47-8cec-7cfd03bac663',
                                     'titleType':'DGame',
                                     'platform':'Durango',
                                     'name':'Game3',
                                     'earnedAchievements':35,
                                     'currentGamerscore':1000,
                                     'maxGamerscore':1000
                                  },
                                     ],
                                   'pagingInfo':{  
                                      'continuationToken':null,
                                      'totalRecords':86
                                   }
                                }";


        var games = JsonConvert.DeserializeObject<Games>(jsonObject);

【讨论】:

    【解决方案2】:

    只需添加一个额外的类来包含您的Titles 集合(或Games,最好下定决心......)

    public class Container
    {
        public Game[] Titles { get; set; }
    }
    

    现在您可以像这样轻松反序列化:

    var res = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Container>(jsonObject);
    

    要使用它,请将System.Web.Extensions 的引用添加到您的项目中。

    请注意,我必须从您的 JSON 中删除一个逗号才能使其正常工作:

     },    <------ this comma should not be there
      ],
    "pagingInfo":{  
       "continuationToken":null,
       "totalRecords":86
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多