问题
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);
The error I am getting is
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 :)
回答1:
You're trying to deserialize array datapoint[]
into datapoint
.
Change
public datapoint datapoints { get; set; }
To
public datapoint[] datapoints { get; set; }
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; }
}
回答2:
You need to declare datapoints
as a collection of some sort (e.g. an IEnumerable
, or an IList
)
class jsonMapper
{
[JsonProperty("target")]
public string target { get; set; }
// This has changed to IEnumerable<datapoint>
[JsonProperty("datapoints")]
public IEnumerable<datapoint> datapoints { get; set; }
}
回答3:
Your classes should be like this :
public class datapoint
{
[JsonProperty("x")]
public int x { get; set; }
[JsonProperty("y")]
public decimal y { get; set; }
}
public class jsonMapper
{
[JsonProperty("target")]
public string target { get; set; }
[JsonProperty("datapoints")]
public List<datapoint> datapoints { get; set; }
}
回答4:
In your json string on datapoints
property for each jsonMapper
object is wrapped within square brackets ([]
), then datapoints
is interpreted as collection object instead of single object.
Your jsonMapper class is
class jsonMapper
{
[JsonProperty("target")]
public string target { get; set; }
[JsonProperty("datapoints")]
public datapoint datapoints { get; set; }
}
and datapoints
property is not array or any collection object.
you can change your property from public datapoint datapoints { get; set; };
to public List<datapoint> datapoints { get; set; };
or you can remove square brackets ([]
) from your json string.
来源:https://stackoverflow.com/questions/49895597/error-in-deserialising-the-objects