简体   繁体   中英

Issue with deserializing list of objects with newtonsfot in .net

I have been knocking my head against the wall for some time because of this issue deserializing a list of objects.

It serializes fine, but the issue arrives when having to load it later.

I've read a lot of questions on stackoverflow and tried putting the actual list into a class of its own and deserializing that, instead of deserializing a list.

public class Guild
{
    [JsonProperty("id")] internal ulong Id { get; set; }
    [JsonProperty("prefix")] internal string Prefix { get; set; }
    [JsonProperty("tracked_games")] internal List<string> TrackedGames { get; set; }
    [JsonProperty("liverole_id")] internal ulong LiveroleId { get; set; }
}

This is the class.

        using (var sw = new StreamWriter("guilds.json"))
        {
            sw.Write(JsonConvert.SerializeObject(_guilds));
        }

(_guilds is a list of Guild)

And this is the serializing. It comes out fine, looks like this:

[{"id":139677590393716737,"prefix":"--","tracked_games":["Factorio"],"liverole_id":0}]

I deserialise with

_guilds = JsonConvert.DeserializeObject<List<Guild>>("guilds.json");

The error that occurs when trying to deserialize is

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: g. Path '', line 0, position 0.
   at Newtonsoft.Json.JsonTextReader.ParseValue() in /_/Src/Newtonsoft.Json/JsonTextReader.cs:line 1772
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) in /_/Src/Newtonsoft.Json/JsonReader.cs:line 1195
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) in /_/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:line 149
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in /_/Src/Newtonsoft.Json/JsonSerializer.cs:line 907
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) in /_/Src/Newtonsoft.Json/JsonConvert.cs:line 828
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) in /_/Src/Newtonsoft.Json/JsonConvert.cs:line 786
   at Squid.Squid.LoadGuilds() in D:\Repos\Projects\Squid\Squid\Squid.cs:line 93

Any help would be very much appreciated. I've spent a long time on this issue and I really cannot see why this is happening. I feel like I'm doing exactly what all the other answers here are doing concerning deserializing a list.

As it is stated by @styx, you need to read your file with StreamReader. you can check the code below:

    using (StreamReader reader = new StreamReader("file.txt"))
    {
        line = reader.ReadToEnd();
    }

When you try to deserialize the json, the JsonConvert.DeserializeObject doesnt work as a StreamReader, you need to read the file first and then deserialize the file content.

        using (StreamReader rdr = new StreamReader("guilds.json"))
        {
            guilds = JsonConvert.DeserializeObject<MainProp>(rdr.ReadToEnd());
        }

This should work for you.

read the file into a string and deserialize JSON to a type

_guilds = JsonConvert.DeserializeObject<List<Guild>>(File.ReadAllText(("guilds.json"));

OR deserialize JSON directly from a file

using (StreamReader file = File.OpenText("guilds.json")) // or new StreamReader(..)
{
    JsonSerializer serializer = new JsonSerializer();
    _guilds = (List<Guild>)serializer.Deserialize(file, typeof(List<Guild>));
}

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