Error while deserializing objects

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 deserialize it into a collection. But I get an error message. Can someone direct me on the right path 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 get is

JSON (, [1,2,3]) "ISSPortal2.datapoint", JSON (, {\ "name \":\ "value \" }) .\r\n , JSON JSON (, {\ "name \":\ "value \" }), , (, ICollection, IList), List, JSON. JsonArrayAttribute , JSON.\R\nPath '[0].datapoints', 1, 40.

. . . , . :)

+4
4

datapoint[] datapoint.

public datapoint datapoints { get; set; }

public datapoint[] datapoints { get; set; }

: #, " " .

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; }
}
+1

datapoints (, IEnumerable IList)

class jsonMapper
{
    [JsonProperty("target")]
    public string target { get; set; }

    // This has changed to IEnumerable<datapoint>
    [JsonProperty("datapoints")]
    public IEnumerable<datapoint> datapoints { get; set; }
}
+1

:

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; }
}
0

In your json string, the datapointsproperty for each object is jsonMapperenclosed in square brackets ( []), then it is datapointsinterpreted as a collection object instead of a single object. JsonMapper class

class jsonMapper
{
    [JsonProperty("target")]
    public string target { get; set; }
    [JsonProperty("datapoints")]
    public datapoint datapoints { get; set; }
}

and the datapointsproperty is not an 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 the square brackets ( []) from the json string.

0
source

Source: https://habr.com/ru/post/1696271/


All Articles