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

标签 c# html json api serialization

我是 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);

关于c# - 在 C# 中将 JSON 数组反序列化为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45896160/

相关文章:

php - 使用 jQuery 选项卡更改变量

c# - WCF服务重启后如何恢复WCF客户端?

c# - 在运行时从 txt 文件更新文本框而不单击 "refresh"

c# - 在 WPF 中打开窗口时的新任务栏图标

html - 推特 Bootstrap : drop-down menu class not working

javascript - JSON.stringify 显示属性值,但无法直接访问属性

c# - 如何执行参数化 SQL 查询(使用带@变量的 LIKE)

html - 通过 CSS 防止 iPad 移动版 Safari 截断内容

arrays - 拆分字符串数组

javascript - Webix - 在数据表中使用 URL 之前如何解析 JSON?