C#-解析值{时遇到意外字符.路径'[0].统计 [英] C# - Unexpected character encountered while parsing value: {. Path '[0].Statistics
问题描述
我正在尝试使用以下代码根据我的模型反序列化JSON对象:
I am trying to deserialize a JSON object according to my model using the code below:
LoadData<MyModel>(Data.Stats, null);
public void LoadData<TModel>(string data, JsonSerializerSettings jsonSettings) where TModel : class
{
var mockData = JsonConvert.DeserializeObject<Collection<TModel>>(data, jsonSettings); // ERROR HERE
Context.SaveChanges();
}
但是我收到一条错误消息
However I am getting an error that reads
Newtonsoft.Json.JsonReaderException:'解析值时遇到意外字符:{.路径'[0] .Statistics',第7行,位置19.'
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '[0].Statistics', line 7, position 19.'
我的JSON对象是:
[
{
"Id": 3033,
"Grade": 3,
"Statistics": { //ERROR OCCURS ON THIS PROPERTY
"Avatar.Add": 1,
"TotalPlays": 36,
"Game.TotalPlays.Spell_Mem_Words": 27,
"Book.TotalReads.Count": 23,
"Game.TotalPlays.Count": 39,
"Character.TotalPlays.L": 23,
"Character.TotalPlays.E": 3,
"TotalPlays.Pick_Vocab": 16,
"Character.TotalPlays.R": 22
}
}
]
对象模型是:
public class MyModel
{
public int Id { get; set; }
public int Grade { get; set; }
public string Statistics { get; set; }
}
我尝试过的事情
(1)使用json皮棉我已经确保json字符串有效.
(1) Using json lint I have ensured that the json string is valid.
(2)在javascript中使用环绕对象的序列号对对象进行序列化.反引号在C#中不起作用 JS小提琴
(2) In javascript serializing the object with back ticks surrounding it works. Backticks don't work in C# JS Fiddle
(3)试图使对象模型中的Statistics属性使用名为stats的类,而不是像这样的字符串
(3) Tried making the Statistics property in object model to use class called stats instead of string like
public class Stats
{
public string Label { get; set;}
public int Value { get; set; }
}
(4)尝试了几乎所有此SO帖子中的答案
(4) Tried nearly all the answers on this SO post
很遗憾,我仍然没有解决此问题.有什么想法吗?
Unfortunately I still have not solved this issue. Any ideas?
推荐答案
我能够重现此MCVE的问题:
I was able to reproduce the problem with this MCVE:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeserializeJson
{
/**
* REFERENCE:
* https://stackoverflow.com/questions/53562566/
*
* ORIGINAL ERROR:
* "Unexpected character encountered while parsing value: {. Path '[0].Statistics', line 5, position 19."
*/
public class Stats
{
public string Label { get; set; }
public int Value { get; set; }
}
public class MyModel
{
public int Id { get; set; }
public int Grade { get; set; }
public string Statistics { get; set; }
}
class Program
{
static Collection<MyModel> LoadData(string data)
{
var retval = JsonConvert.DeserializeObject<Collection<MyModel>>(data);
return retval;
}
static void Main(string[] args)
{
try
{
string s = File.ReadAllText(@"test-data.json");
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented
};
Collection <MyModel> mockData = Program.LoadData(s);
System.Console.WriteLine("#/items= " + mockData.Count);
foreach (MyModel item in mockData)
{
System.Console.WriteLine(" id= {0}, Grade={1}, Statistics={2}", item.Id, item.Grade, item.Statistics.ToString());
}
}
catch (Exception ex)
{
System.Console.WriteLine("ERROR:", ex);
}
}
}
}
我能够通过以下方式修复它:
I was able to fix it by:
- 详细说明
class Stats
,然后 -
在
class MyModel
的定义中使用统计信息:
- Elaborating your definition of
class Stats
, then Using Stats in the definition of
class MyModel
:
public class Stats
{
public int AvatarAdd { get; set; }
public int TotalPlays { get; set; }
public int GameTotalPlaysSpellMemWords { get; set; }
public int BookTotalReadsCount { get; set; }
public int GameTotalPlaysCount { get; set; }
public int CharacterTotalPlaysL { get; set; }
public int CharacterTotalPlaysE { get; set; }
public int TotalPlaysPick_Vocab { get; set; }
public int CharacterTotalPlaysR { get; set; }
}
public class MyModel
{
public int Id { get; set; }
public int Grade { get; set; }
public Stats Statistics { get; set; }
}
您有多种选择(包括逐字使用上述示例).我的建议是将统计"细分为较小的模型类.
You have several choices (including use the above example verbatim). My suggestion would be to break "Statistics" down into smaller model classes.
这篇关于C#-解析值{时遇到意外字符.路径'[0].统计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!