4

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 :)

syam
  • 799
  • 1
  • 12
  • 30
  • 1
    `public datapoint datapoints { get; set; }` should be something like `List datapoints { get; set; }` ...? – Paul Suart Apr 18 '18 at 09:13
  • 2
    Top tip: You can copy the JSON string to the clipboard and then use Visual Studio 2017 to paste it into a source code file via `Edit | Paste Special | Paste JSON as Classes` and it will give you the right class declarations (although you will likely have to fiddle around with the results a bit). – Matthew Watson Apr 18 '18 at 09:18

4 Answers4

1

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; }
}
RB.
  • 36,301
  • 12
  • 91
  • 131
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; }
}
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

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; }
}
Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
0

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.

Chandra Ari Gunawan
  • 143
  • 1
  • 1
  • 10