1

嘿,我想反序列化这个 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;
    }
4

6 回答 6

6

将此用作模型类

 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
于 2018-06-20T14:51:01.833 回答
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);
    }
}
于 2018-06-20T14:42:02.953 回答
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 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; }
}
于 2018-06-20T14:42:09.007 回答
0
  1. 打开 nuget,搜索 newtonsoft.json 并安装。
  2. 反序列化:

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

于 2018-06-20T14:44:39.953 回答
0

您需要更改对象结构:

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

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

然后反序列化 Response1 (就像你现在做的那样)

于 2018-06-20T14:51:58.927 回答
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[])));
于 2018-06-20T15:07:12.297 回答