C#反序列化json api响应

C#反序列化json api响应,c#,json.net,C#,Json.net,嘿,我想反序列化这个json API响应,以获取包括概要文件状态等在内的值,以便在程序中进行处理。 在这里,我从不同的问题中尝试了多种方法,但得到的回答为空。 这是代码,请纠正我做错了什么 { "response": { "players": [{ "steamid": "xxxxxxxxxxxxxxxxx", "communityvisibilitystate": 3, "profilestate"

嘿,我想反序列化这个json API响应,以获取包括概要文件状态等在内的值,以便在程序中进行处理。 在这里,我从不同的问题中尝试了多种方法,但得到的回答为空。 这是代码,请纠正我做错了什么

{
    "response": {
        "players": [{
            "steamid": "xxxxxxxxxxxxxxxxx",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "xxxx xxxx",
            "lastlogoff": 1529478555,
            "commentpermission": 1,
            "profileurl": "xxxxxxxxxxxxxxx",
            "avatar": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "avatarmedium": "xxxxxxxxxxxxxxxxxxxxx",
            "avatarfull": "xxxxxxxxxxx",
            "personastate": 1,
            "realname": "xxxx",
            "primaryclanid": "xxxxxxxxxx",
            "timecreated": 1097464215,
            "personastateflags": 0
        }]
    }
}
我试过的代码

public class AccountInfo
    {
        public string steamid { get; set; }
        public int communityvisibilitystate { get; set; }
        public int profilestate { get; set; }
        public string personaname { get; set; }
        public ulong lastlogoff { get; set; }
        public int commentpermission { get; set; }
        public string profileurl { get; set; }
        public string avatar { get; set; }
        public string avatarmedium { get; set; }
        public string avatarfull { get; set; }
        public int personastate { get; set; }
        public string realname { get; set; }
        public string primaryclanid { get; set; }
        public ulong timecreated { get; set; }
        public int personastateflags { get; set; }
    }

    public class Response
    {
        public AccountInfo response { get; set; }
    }

    public class Response1
    {
        public Response players { get; set; }
    }

    static void Main(string[] args)
    {
        DeserilizeJson();
    }

    internal static void DeserilizeJson()
    {
        string json = GetUrlToString("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
        Console.WriteLine(json);
        Response1 info = JsonConvert.DeserializeObject<Response1>(json);

        using (StreamWriter file = File.CreateText(@"c:\test.json"))
        {
            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(file, info);
        }
    }

    internal static string GetUrlToString(string url)
    {
        String Response = null;

        try
        {
            using (WebClient client = new WebClient())
            {
                Response = client.DownloadString(url);
            }
        }
        catch (Exception)
        {                
            return null;
        }

        return Response;
    }
公共类AccountInfo
{
公共字符串streamid{get;set;}
公共int社区可见性状态{get;set;}
公共int profilestate{get;set;}
公共字符串名称{get;set;}
public ulong lastlogoff{get;set;}
public int commentpermission{get;set;}
公共字符串配置文件URL{get;set;}
公共字符串avatar{get;set;}
公共字符串{get;set;}
公共字符串avatarfull{get;set;}
公共状态{get;set;}
公共字符串实名{get;set;}
公共字符串primaryclanid{get;set;}
公共ulong timecreated{get;set;}
公共int状态标志{get;set;}
}
公众课堂反应
{
公共AccountInfo响应{get;set;}
}
公共类响应1
{
公共响应播放器{get;set;}
}
静态void Main(字符串[]参数)
{
反序列化JSON();
}
内部静态void反序列化JSON()
{
字符串json=GetUrlToString(“http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
Console.WriteLine(json);
Response1 info=JsonConvert.DeserializeObject(json);
使用(StreamWriter file=file.CreateText(@“c:\test.json”))
{
JsonSerializer serializer=新的JsonSerializer();
序列化器。序列化(文件、信息);
}
}
内部静态字符串GetUrlToString(字符串url)
{
字符串响应=null;
尝试
{
使用(WebClient=newWebClient())
{
Response=client.DownloadString(url);
}
}
捕获(例外)
{                
返回null;
}
返回响应;
}
试试这个:

public class Player
{
    public string steamid { get; set; }
    public int communityvisibilitystate { get; set; }
    public int profilestate { get; set; }
    public string personaname { get; set; }
    public ulong lastlogoff { get; set; }
    public int commentpermission { get; set; }
    public string profileurl { get; set; }
    public string avatar { get; set; }
    public string avatarmedium { get; set; }
    public string avatarfull { get; set; }
    public int personastate { get; set; }
    public string realname { get; set; }
    public string primaryclanid { get; set; }
    public ulong timecreated { get; set; }
    public int personastateflags { get; set; }    
}

public class Response
{
    public Player[] players { get; set; }
}

public class EncapsulatedResponse
{
    public Response response {get;set;}
}

internal static void DeserilizeJson()
{
    string json = GetUrlToString("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
    Console.WriteLine(json);
    EncapsulatedResponse info = JsonConvert.DeserializeObject<EncapsulatedResponse>(json);

    using (StreamWriter file = File.CreateText(@"c:\test.json"))
    {
        JsonSerializer serializer = new JsonSerializer();
        serializer.Serialize(file, info);
    }
}
公共类播放器
{
公共字符串streamid{get;set;}
公共int社区可见性状态{get;set;}
公共int profilestate{get;set;}
公共字符串名称{get;set;}
public ulong lastlogoff{get;set;}
public int commentpermission{get;set;}
公共字符串配置文件URL{get;set;}
公共字符串avatar{get;set;}
公共字符串{get;set;}
公共字符串avatarfull{get;set;}
公共状态{get;set;}
公共字符串实名{get;set;}
公共字符串primaryclanid{get;set;}
公共ulong timecreated{get;set;}
公共int状态标志{get;set;}
}
公众课堂反应
{
公共播放器[]播放器{get;set;}
}
公共类封装的响应
{
公共响应{get;set;}
}
内部静态void反序列化JSON()
{
字符串json=GetUrlToString(“http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
Console.WriteLine(json);
EncapsedResponse info=JsonConvert.DeserializeObject(json);
使用(StreamWriter file=file.CreateText(@“c:\test.json”))
{
JsonSerializer serializer=新的JsonSerializer();
序列化器。序列化(文件、信息);
}
}
试试这个:

public class Player
{
    public string steamid { get; set; }
    public int communityvisibilitystate { get; set; }
    public int profilestate { get; set; }
    public string personaname { get; set; }
    public ulong lastlogoff { get; set; }
    public int commentpermission { get; set; }
    public string profileurl { get; set; }
    public string avatar { get; set; }
    public string avatarmedium { get; set; }
    public string avatarfull { get; set; }
    public int personastate { get; set; }
    public string realname { get; set; }
    public string primaryclanid { get; set; }
    public ulong timecreated { get; set; }
    public int personastateflags { get; set; }    
}

public class Response
{
    public Player[] players { get; set; }
}

public class EncapsulatedResponse
{
    public Response response {get;set;}
}

internal static void DeserilizeJson()
{
    string json = GetUrlToString("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
    Console.WriteLine(json);
    EncapsulatedResponse info = JsonConvert.DeserializeObject<EncapsulatedResponse>(json);

    using (StreamWriter file = File.CreateText(@"c:\test.json"))
    {
        JsonSerializer serializer = new JsonSerializer();
        serializer.Serialize(file, info);
    }
}
公共类播放器
{
公共字符串streamid{get;set;}
公共int社区可见性状态{get;set;}
公共int profilestate{get;set;}
公共字符串名称{get;set;}
public ulong lastlogoff{get;set;}
public int commentpermission{get;set;}
公共字符串配置文件URL{get;set;}
公共字符串avatar{get;set;}
公共字符串{get;set;}
公共字符串avatarfull{get;set;}
公共状态{get;set;}
公共字符串实名{get;set;}
公共字符串primaryclanid{get;set;}
公共ulong timecreated{get;set;}
公共int状态标志{get;set;}
}
公众课堂反应
{
公共播放器[]播放器{get;set;}
}
公共类封装的响应
{
公共响应{get;set;}
}
内部静态void反序列化JSON()
{
字符串json=GetUrlToString(“http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
Console.WriteLine(json);
EncapsedResponse info=JsonConvert.DeserializeObject(json);
使用(StreamWriter file=file.CreateText(@“c:\test.json”))
{
JsonSerializer serializer=新的JsonSerializer();
序列化器。序列化(文件、信息);
}
}

玩家属性应该是一个玩家列表,因为它是一个数组。下面是正确的模型

 public class Player
{
    public string steamid { get; set; }
    public int communityvisibilitystate { get; set; }
    public int profilestate { get; set; }
    public string personaname { get; set; }
    public int lastlogoff { get; set; }
    public int commentpermission { get; set; }
    public string profileurl { get; set; }
    public string avatar { get; set; }
    public string avatarmedium { get; set; }
    public string avatarfull { get; set; }
    public int personastate { get; set; }
    public string realname { get; set; }
    public string primaryclanid { get; set; }
    public int timecreated { get; set; }
    public int personastateflags { get; set; }
}

public class Response
{
    public List<Player> players { get; set; }
}
公共类播放器
{
公共字符串streamid{get;set;}
公共int社区可见性状态{get;set;}
公共int profilestate{get;set;}
公共字符串名称{get;set;}
public int lastlogoff{get;set;}
public int commentpermission{get;set;}
公共字符串配置文件URL{get;set;}
公共字符串avatar{get;set;}
公共字符串{get;set;}
公共字符串avatarfull{get;set;}
公共状态{get;set;}
公共字符串实名{get;set;}
公共字符串primaryclanid{get;set;}
public int timecreated{get;set;}
公共int状态标志{get;set;}
}
公众课堂反应
{
公共列表玩家{get;set;}
}

玩家属性应该是一个玩家列表,因为它是一个数组。下面是正确的模型

 public class Player
{
    public string steamid { get; set; }
    public int communityvisibilitystate { get; set; }
    public int profilestate { get; set; }
    public string personaname { get; set; }
    public int lastlogoff { get; set; }
    public int commentpermission { get; set; }
    public string profileurl { get; set; }
    public string avatar { get; set; }
    public string avatarmedium { get; set; }
    public string avatarfull { get; set; }
    public int personastate { get; set; }
    public string realname { get; set; }
    public string primaryclanid { get; set; }
    public int timecreated { get; set; }
    public int personastateflags { get; set; }
}

public class Response
{
    public List<Player> players { get; set; }
}
公共类播放器
{
公共字符串streamid{get;set;}
公共int社区可见性状态{get;set;}
公共信息档案
public class Response
{
    public AccountInfo[] players { get; set; }
}

public class Response1
{
    public Response response { get; set; }
}
var accounts = JObject.Parse(json).Root
    .SelectToken("response.players")
    .ToObject(typeof(AccountInfo[]));
var accounts = JObject.Parse(json)["response"]["players"]
    .ToObject((typeof(AccountInfo[])));