简体   繁体   English

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

[英]Deserializing JSON Array to Object in C#

I'm reletively new to C# (previous experience with HTML/JS/Angular) and I am having issues with deserializing the JSON i'm reciving back from an API I am using. 我对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
   }
}

The issue is I am not sure how to deserialize this in to an array of objects. 问题是我不知道如何将其反序列化为一个对象数组。

I have created an object class: 我创建了一个对象类:

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; }
    }

From there i've tried using JsonConvert.DeserializeObject(result) but this just returns "CompleteAdmin.Controllers.AchievementsAPIController+Game" which isn't usable. 从那里我尝试使用JsonConvert.DeserializeObject(结果),但这只返回“CompleteAdmin.Controllers.AchievementsAPIController + Game”,这是不可用的。

Can anybody show me how this is supposed to be setup? 任何人都可以告诉我这是如何设置的吗? Ultimately i'm aiming to get this in to a DB. 最终我的目标是将其传入数据库。 :) :)

Thanks. 谢谢。

Its simple like 它很简单

in visual studio right click on the solution and choose "Manage NuGet Packages" a menu will be open in the top search type "newtonsoft" select the very first option with black icon. 在visual studio中右键单击解决方案并选择“Manage NuGet Packages”,将在顶部搜索类型“newtonsoft”中打开一个菜单,选择带有黑色图标的第一个选项。 and add to your project. 并添加到您的项目。 then write the following. 然后写下面的内容。

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; }
}

On page load or where you want the result : 在页面加载或您想要结果的位置:

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

Just add an extra class to contain your collection of Titles (or Games , better make up your mind...) 只需添加一个额外的类来包含您的Titles (或Games ,更好地下定决心......)

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

Now you can easily deserialize like this: 现在您可以像这样轻松地反序列化:

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

To use this, add a reference to System.Web.Extensions to your project. 要使用它,请将System.Web.Extensions的引用添加到项目中。

Note that I had to remove a comma from your JSON to get this to work: 请注意,我必须从您的JSON中删除一个逗号才能使其工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM