反序列化对象时出错 [英] Error in deserialising the objects
问题描述
我有一个JSON字符串.
I have a JSON string.
[
{
"target":"FDOL00001",
"datapoints":[
{
"y":72.564,
"x":1523858700
}
]
},
{
"target":"FDOL00001",
"datapoints":[
{
"y":86.366,
"x":1523858700
}
]
},
{
"target":"FDOL00001",
"datapoints":[
{
"y":73.90195818815343,
"x":1523858700
}
]
}
]
我正在尝试将其反序列化为一个集合.但是我遇到了一个错误.有人可以指导我采取正确的方法解决此问题吗?
I am trying to deserialise it to a collection. But I am getting an error. Can somebody direct me to the right way to fix this?
class datapoint
{
[JsonProperty("x")]
public int x { get; set; }
[JsonProperty("y")]
public decimal y { get; set; }
}
class jsonMapper
{
[JsonProperty("target")]
public string target { get; set; }
[JsonProperty("datapoints")]
public datapoint datapoints { get; set; }
}
我正在尝试使用以下代码进行转换.
I am trying to convert using the following code.
var json = JsonConvert.DeserializeObject<List<jsonMapper>>(objText);
我得到的错误是
无法将当前JSON数组(例如[1,2,3])反序列化为类型"ISSPortal2.datapoint",因为该类型需要一个JSON对象(例如{\"name \":\"value \"}) \ r \ n要解决此错误,请将JSON更改为JSON对象(例如{\"name \":\"value \"})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如可以从JSON数组反序列化的List.也可以将JsonArrayAttribute添加到该类型中,以强制其从JSON数组反序列化.\ r \ nPath'[0] .datapoints',第1行,位置40.
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ISSPortal2.datapoint' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '[0].datapoints', line 1, position 40.
I already checked this . But its not working. What's wrong in my code. Please help me to identify that. Thanks in advance :)
推荐答案
您正在尝试将数组datapoint[]
反序列化为datapoint
.
You're trying to deserialize array datapoint[]
into datapoint
.
更改
public datapoint datapoints { get; set; }
收件人
public datapoint[] datapoints { get; set; }
奖金::C#命名约定,建议您在公共财产中使用大写字母.
Bonus: C# Naming Conventions, suggest you to use Capital Letters in public properties.
class Datapoint
{
[JsonProperty("x")]
public int X { get; set; }
[JsonProperty("y")]
public decimal Y { get; set; }
}
class JsonMapper
{
[JsonProperty("target")]
public string Target { get; set; }
[JsonProperty("datapoints")]
public Datapoint Datapoints { get; set; }
}
这篇关于反序列化对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!