在 C# 中将 JSON 数组反序列化为对象



我是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",这是不可用的。

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

谢谢。

它很简单

在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);

只需添加一个额外的类来包含您的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
}

最新更新