C#反序列化JSON API响应

时间:2018-06-20 14:35:50

标签: c# json.net

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

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

6 个答案:

答案 0 :(得分:2)

将此用作模型类

 using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class JsonModel
    {
        [JsonProperty("response")]
        public Response Response { get; set; }
    }

    public partial class Response
    {
        [JsonProperty("players")]
        public List<Player> Players { get; set; }
    }

    public partial class Player
    {
        [JsonProperty("steamid")]
        public string Steamid { get; set; }

        [JsonProperty("communityvisibilitystate")]
        public long Communityvisibilitystate { get; set; }

        [JsonProperty("profilestate")]
        public long Profilestate { get; set; }

        [JsonProperty("personaname")]
        public string Personaname { get; set; }

        [JsonProperty("lastlogoff")]
        public long Lastlogoff { get; set; }

        [JsonProperty("commentpermission")]
        public long Commentpermission { get; set; }

        [JsonProperty("profileurl")]
        public string Profileurl { get; set; }

        [JsonProperty("avatar")]
        public string Avatar { get; set; }

        [JsonProperty("avatarmedium")]
        public string Avatarmedium { get; set; }

        [JsonProperty("avatarfull")]
        public string Avatarfull { get; set; }

        [JsonProperty("personastate")]
        public long Personastate { get; set; }

        [JsonProperty("realname")]
        public string Realname { get; set; }

        [JsonProperty("primaryclanid")]
        public string Primaryclanid { get; set; }

        [JsonProperty("timecreated")]
        public long Timecreated { get; set; }

        [JsonProperty("personastateflags")]
        public long Personastateflags { get; set; }
    }

然后在您的主班学习

     var info = JsonConvert.DeserializeObject<JsonModel>(json);
var Response = info.Response

答案 1 :(得分:0)

尝试一下:

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

答案 2 :(得分:0)

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

REPLACE INTO

答案 3 :(得分:0)

  1. 打开nuget,搜索newtonsoft.json并安装。
  2. 反序列化:

    var反序列化= JsonConvert.DeserializeObject(jsonstring);

答案 4 :(得分:0)

您需要更改对象结构:

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

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

然后反序列化Response1(就像您当前所做的那样)

答案 5 :(得分:0)

只需提供一种不同的方法,就可以使用JObject(Newtonsoft.Json.Linq),以便只需要AccountInfo类:

var accounts = JObject.Parse(json).Root
    .SelectToken("response.players")
    .ToObject(typeof(AccountInfo[]));

或者在某些情况下,导航属性甚至更容易:

var accounts = JObject.Parse(json)["response"]["players"]
    .ToObject((typeof(AccountInfo[])));
相关问题