C# C Newtonsoft JsonSerializationException反序列化序列化字符串时出现异常
我一定是犯了个愚蠢的错误。当我跑步时:C# C Newtonsoft JsonSerializationException反序列化序列化字符串时出现异常,c#,json.net,json-deserialization,C#,Json.net,Json Deserialization,我一定是犯了个愚蠢的错误。当我跑步时: JsonResponse testREsponse = new JsonResponse { StartTimeUtc = 1, EndTimeUtc = 1, TimeResolutionInMilliseconds = 60000, Results = new JsonResults
JsonResponse testREsponse = new JsonResponse
{
StartTimeUtc = 1,
EndTimeUtc = 1,
TimeResolutionInMilliseconds = 60000,
Results = new JsonResults
{
Type = "hello",
Values = new List<EvaluatedResult>()
}
};
string convertTest = JsonConvert.SerializeObject(testREsponse);
Console.WriteLine("HRMM " + convertTest);
JsonResponse jsonResponse = JsonConvert.DeserializeObject<JsonResponse>(convertTest);
当尝试反序列化我序列化了上面一行的字符串时。它表示“路径”结果。$values',第1行,位置71'。这将是结果括号
对象:
class JsonResponse
{
[JsonProperty("startTimeUtc")]
public long StartTimeUtc { get; set; }
[JsonProperty("endTimeUtc")]
public long EndTimeUtc { get; set; }
[JsonProperty("results")]
public JsonResults Results { get; set; }
[JsonProperty("timeResolutionInMilliseconds")]
public int TimeResolutionInMilliseconds { get; set; }
}
class JsonResults
{
[JsonProperty("$type")]
public string Type { get; set; }
[JsonProperty("$values")]
public List<EvaluatedResult> Values { get; set; }
}
class EvaluatedResult
{
[JsonProperty("dimensionList")]
public DimensionList dimensionList { get; set; }
[JsonProperty("evaluatedResult")]
public decimal evaluatedResult { get; set; }
[JsonProperty("seriesValues")]
public List<Decimal> seriesValues { get; set; }
}
class DimensionList
{
[JsonProperty("$type")]
public string type { get; set; }
[JsonProperty("$values")]
public List<Dimension> values;
}
class Dimension
{
[JsonProperty("key")]
public string key { get; set; }
[JsonProperty("value")]
public string value { get; set; }
}
当我从构造中删除JsonResults时,它可以很好地序列化和反序列化。我错过了什么?提前感谢。请尝试以下操作:
JsonConvert.DeserializeObject<JsonResponse>(convertTest, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore });
$表示元数据。序列化程序设置使该值被视为属性。oh my。。。非常感谢你。2天的沮丧情绪得到解决。我不明白为什么使用JsonProperty$value进行测试是可行的,但不是“$values”,而是实际使用需要“$values”。谢谢我甚至从未想过要考虑元数据。
{"startTimeUtc":1,"endTimeUtc":1,"results":{"$type":"hello","$values":[]},"timeResolutionInMilliseconds":60000}
JsonConvert.DeserializeObject<JsonResponse>(convertTest, new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore });