简体   繁体   中英

How can I deserialize this specific json string?

I am trying to deserialize the following json string using Newtonsoft Json. I am able to get the string successfully, but when I try using JsonConvert.DeserializeObject<ServerList>(response, settings); , the try catch fails.

[
{"endpoint":"127.0.0.1","id":6,"identifiers":["steam:","license:","xbl:","live:","discord:"],"name":"Blurr","ping":160},
{"endpoint":"127.0.0.1","id":7,"identifiers":["steam:","license:","xbl:","live:","discord:"],"name":"Knight","ping":120}
]

I believe my issue is because the players array being unnamed.

I have tried [JsonProperty("")] and [JsonProperty] for the Users var, I have also tried using List and Array instead of IList.

Here is the object. This may be completely wrong, I have tried many ways of doing this.

    public class ServerList
    {
        // I have tried many ways of doing this array/list. This is just the latest way I tried.
        [JsonProperty]
        public static IList<Player> Users { get; set; }
    }

    public class Player
    {
        [JsonProperty("endpoint")]
        public static string Endpoint { get; set; }

        [JsonProperty("id")]
        public static string ServerId { get; set; }

        [JsonProperty("identifiers")]
        public static IList<string> Identifiers { get; set; }

        [JsonProperty("name")]
        public static string Name { get; set; }

        [JsonProperty("ping")]
        public static int Ping { get; set; }
    }

I am expecting to get a 'ServerList' object returned with a list of all the players connected to the server.

Ask any questions you need to, I don't often work with json in this format. Thank you in advance!

ERROR: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.

Simplest way: Your json is an array, so deserialize to an array:

JsonConvert.DeserializeObject<Player[]>(response,settings);

As noted in the comments, properties on the Player should not be static.

If you insist on an object structure similar to what you posted, an object with a Users property, even though it's not present in the JSON, that is also possible, by implementing a custom JSON converter. There's an article with an example of that here: https://www.jerriepelser.com/blog/custom-converters-in-json-net-case-study-1/

HOWEVER; I would recommend sticking to the types present in the json, and perhaps later construct the object that makes sense to the model in your program. This way, what you deserialize are true to the json you are getting, but make no sacrifices on the model you'd like:

var players = JsonConvert.DeserializeObject<Player[]>(response,settings);
var serverList = new ServerList {Users = players};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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