Json 反序列化错误 „'W' 是值的无效开始
Json deserialization error „'W' is an invalid start of a value
我需要在控制台中创建一个简单的游戏。我有序列化 JSON,当我想反序列化这个文件时,我有这个错误:
System.Text.Json.JsonException: „'W' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.”
这是我的代码 类:
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 文件
{
"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",
]
}
]
}
这是我用来反序列化 json 文件的代码:
string fileName = "Local.json";
var options = new JsonSerializerOptions { AllowTrailingCommas = true };
var result = JsonSerializer.Deserialize<Root>(fileName, options);
我没有对 JSON 文件进行序列化和反序列化。
提前致谢。
您可以使用以下 Model
结构反序列化您的 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; }
}
在您之前的 you were advised to 中将属性重命名为 Person
和 Monster
。
假设这没有恢复,那么这里的问题是您如何读取和尝试反序列化文件。您需要打开文件的内容然后反序列化,但您错误地尝试反序列化 fileName
字符串。正确代码如下:
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);
我需要在控制台中创建一个简单的游戏。我有序列化 JSON,当我想反序列化这个文件时,我有这个错误:
System.Text.Json.JsonException: „'W' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.”
这是我的代码 类:
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 文件
{
"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",
]
}
]
}
这是我用来反序列化 json 文件的代码:
string fileName = "Local.json";
var options = new JsonSerializerOptions { AllowTrailingCommas = true };
var result = JsonSerializer.Deserialize<Root>(fileName, options);
我没有对 JSON 文件进行序列化和反序列化。 提前致谢。
您可以使用以下 Model
结构反序列化您的 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; }
}
在您之前的 Person
和 Monster
。
假设这没有恢复,那么这里的问题是您如何读取和尝试反序列化文件。您需要打开文件的内容然后反序列化,但您错误地尝试反序列化 fileName
字符串。正确代码如下:
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);