【发布时间】: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”。
谁能告诉我这应该如何设置?最终,我的目标是将其放入数据库。 :)
谢谢。
【问题讨论】:
-
T这是使用Json.NET的方法newtonsoft.com/json/help/html/DeserializeObject.htm
-
据我所知,这几乎是我当前的设置:games = JsonConvert.DeserializeObject
(result); -
如果您的 JSON 是一个集合,那么它将是 JsonConvert.DeserializeObject
- > 或任何集合。
-
请注意,在尝试反序列化您的 JSON 时,我收到一条错误消息,提示不应该存在尾随逗号。这是从
pagingInfo倒数的第二个。
标签: c# html json api serialization