Home > Software engineering >  Json deserialization error „'W' is an invalid start of a value
Json deserialization error „'W' is an invalid start of a value

Time:02-14

I need to create a simple game in Console. I have Serializing JSON and when i want to deserialize this file i have this error: System.Text.Json.JsonException: „'W' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.”

This is code of my classes:

public class Person
{
    public string Speciality { get; set; }
    public int PWZ { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public List<string> Items { get; set; }
}

public class Monster
{
    public string Name { get; set; }
    public List<string> Skills { get; set; }
}

public class Root
{
    public List<Person> people { get; set; }
    public List<Monster> monsters { get; set; }
}

Json file

{
  "Person": [
    {
      "Speciality": "Archer",
      "PWZ": 432742,
      "Name": "Charlie",
      "Surname": "Evans",
      "Items": [
        "Bow",
        "Arrow",
      ]
    },
    {
      "Speciality": "Soldier",
      "PWZ": 432534879,
      "Name": "Harry",
      "Surname": "Thomas",
      "Items": [
        "Gun",
        "Knife",
     ]
    }
  ],
  "Monster": [
    {
      "Name": "Papua",
      "Skills": [
        "Jump",
        "SlowWalk",
      ]
    },
    {
     "Name": "Geot",
      "Skills": [
        "Run",
        "Push",
      ]
    }
  ]
}

And it's my code used to deserialize json file:

string fileName = "Local.json";
var options = new JsonSerializerOptions { AllowTrailingCommas = true };
var result = JsonSerializer.Deserialize<Root>(fileName, options);

I haven't worked with serialize and deserialize with JSON files. Thanks in advance.

CodePudding user response:

In your previous question you were advised to update the model to rename the properties to Person and Monster.

Assuming this hasn't reverted, then the issue here is how you're reading and trying to deserialize the file. You need to open the contents of the file then deserialize, but you're mistakenly trying to deserialize the fileName string. Correct code below:

string fileName = "Local.json";
// open the file contents
string jsonContents = File.ReadAllText(fileName);
var options = new JsonSerializerOptions { AllowTrailingCommas = true };
// deserialize the file contents, not the fileName
var result = JsonSerializer.Deserialize<Root>(jsonContents, options);

CodePudding user response:

You can use the following Model structure to deserialize your JSON:

using System;
using System.Collections.Generic;
using System.Text.Json;
                    
public class Program
{
    public static void Main()
    {
        var myString=@"{'Person':[{'Speciality':'Archer','PWZ':432742,'Name':'Charlie','Surname':'Evans','Items':['Bow','Arrow']},{'Speciality':'Soldier','PWZ':432534879,'Name':'Harry','Surname':'Thomas','Items':['Gun','Knife']}],'Monster':[{'Name':'Papua','Skills':['Jump','SlowWalk']},{'Name':'Geot','Skills':['Run','Push']}]}";
        var options = new JsonSerializerOptions { AllowTrailingCommas = true };
        var result = JsonSerializer.Deserialize<Root>(myString, options);
        foreach(var item in result.Person)
        {
            Console.WriteLine(item.Name);
        }
    }
}

public class Person
{
    public string Speciality { get; set; }
    public int PWZ { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public List<string> Items { get; set; }
}

public class Monster
{
    public string Name { get; set; }
    public List<string> Skills { get; set; }
}

public class Root
{
    public List<Person> Person { get; set; }
    public List<Monster> Monster { get; set; }
}
  • Related