解析值 ASP.NET Core 和 Newtonsoft 时遇到意外字符
Unexpected character encountered while parsing value ASP.NET Core and Newtonsoft
我试图在我的 ASP.NET 核心应用程序中使用 Newtonsoft 将 JSON 文件解析为对象,但我不断收到此错误:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path 'palette.swatch', line 7, position 15.'
我试过使用 StreamReader
class,现在 MemoryStream
class。我已经确认在线验证 JSON 并检查它也是 UTF-8。两种方式我都收到相同的错误。我还手动检查了 JSON 但我看不到任何会使其不可读的内容。
这是抛出异常的方法:
public async Task<int> PostDefinition(IFormFile file)
{
MemoryStream ms = new MemoryStream();
file.CopyTo(ms);
byte[] bytes = ms.ToArray();
string s = Encoding.UTF8.GetString(bytes);
Definition definition = Newtonsoft.Json.JsonConvert.DeserializeObject<Definition>(s);
Definition x = ObjectMapper.Map<Definition>(Definition);
return await _definitions.InsertAndGetIdAsync(x);
}
这是抛出错误的 JSON 片段:
{
"name": "Definition",
"id": 2,
"palette": {
"name": "Test Palette",
"default": false,
"swatch": {
"colors": {
"Color 1": "transparent",
"Color 2": "transparent",
"Color 3": "transparent",
"Color 4": "transparent",
"Color 5": "transparent",
"Color 6": "transparent",
"Color 7": "transparent",
"Color 8": "transparent"
}
}
}
}
什么可能导致异常,我可以尝试如何补救?我试过搜索,但似乎没有任何效果。
这对我有用
Definition definition = Newtonsoft.Json.JsonConvert.DeserializeObject<Definition>(s);
类
public partial class Definition
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("palette")]
public Palette Palette { get; set; }
}
public partial class Palette
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("default")]
public bool Default { get; set; }
[JsonProperty("swatch")]
public Swatch Swatch { get; set; }
}
public partial class Swatch
{
[JsonProperty("colors")]
public Dictionary<string,string> Colors { get; set; }
}
我试图在我的 ASP.NET 核心应用程序中使用 Newtonsoft 将 JSON 文件解析为对象,但我不断收到此错误:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path 'palette.swatch', line 7, position 15.'
我试过使用 StreamReader
class,现在 MemoryStream
class。我已经确认在线验证 JSON 并检查它也是 UTF-8。两种方式我都收到相同的错误。我还手动检查了 JSON 但我看不到任何会使其不可读的内容。
这是抛出异常的方法:
public async Task<int> PostDefinition(IFormFile file)
{
MemoryStream ms = new MemoryStream();
file.CopyTo(ms);
byte[] bytes = ms.ToArray();
string s = Encoding.UTF8.GetString(bytes);
Definition definition = Newtonsoft.Json.JsonConvert.DeserializeObject<Definition>(s);
Definition x = ObjectMapper.Map<Definition>(Definition);
return await _definitions.InsertAndGetIdAsync(x);
}
这是抛出错误的 JSON 片段:
{
"name": "Definition",
"id": 2,
"palette": {
"name": "Test Palette",
"default": false,
"swatch": {
"colors": {
"Color 1": "transparent",
"Color 2": "transparent",
"Color 3": "transparent",
"Color 4": "transparent",
"Color 5": "transparent",
"Color 6": "transparent",
"Color 7": "transparent",
"Color 8": "transparent"
}
}
}
}
什么可能导致异常,我可以尝试如何补救?我试过搜索,但似乎没有任何效果。
这对我有用
Definition definition = Newtonsoft.Json.JsonConvert.DeserializeObject<Definition>(s);
类
public partial class Definition
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("palette")]
public Palette Palette { get; set; }
}
public partial class Palette
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("default")]
public bool Default { get; set; }
[JsonProperty("swatch")]
public Swatch Swatch { get; set; }
}
public partial class Swatch
{
[JsonProperty("colors")]
public Dictionary<string,string> Colors { get; set; }
}