-1

This line is giving me the error

Cannot deserialize the current JSON object (e.g. {"name":"value"})

From other posts I gather I should not be putting this into a list. However This worked fine for me until I added the avgPx field.

  1. How can I get this information into my List properly?
  2. Does my list of type <OrderRecord> need to include all the fields returned by the JSON?
List<OrderRecord> orderRecord_Single = new List<OrderRecord>();//define and set to null
OrderRecord_Single = JsonConvert.DeserializeObject<List<OrderRecord>>(orderString);

This is one case of my jsonstring. It has the brackets on it.

"[{\"orderID\":\"5dcc6560-9672-958d-010b-7d18c9d523ab\",\"account\":1024235,\"symbol\":\"ETHUSD\",\"timestamp\":\"2020-04-26T18:21:05.703Z\",\"clOrdID\":\"\",\"side\":\"Buy\",\"price\":194.95,\"orderQty\":1,\"ordStatus\":\"New\",\"text\":\"ZT\",\"transactTime\":\"2020-04-26T18:21:05.703Z\",\"avgPx\":null}]"
public class OrderRecord
{
    [JsonProperty("orderID")]
    public string orderID { get; set; }
    [JsonProperty("symbol")]
    public string symbol { get; set; }
    [JsonProperty("side")]
    public string side { get; set; }
    [JsonProperty("price")]
    public string price { get; set; }
    [JsonProperty("orderQty")]
    public string orderQty { get; set; }
    [JsonProperty("ordStatus")]
    public string ordStatus { get; set; }
    [JsonProperty("transactTime")]
    public string transactTime { get; set; }
    [JsonProperty("timestamp")]
    public string timestamp { get; set; }
    [JsonProperty("avgPx")]
    public string avgPx { get; set; }
}
Brian K
  • 11
  • 4
  • Duplicate of: [Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List\`1](https://stackoverflow.com/questions/21358493/cannot-deserialize-the-current-json-object-e-g-namevalue-into-type-sy) ...and as many as 8840 others on this site alone – Ňɏssa Pøngjǣrdenlarp Apr 25 '20 at 23:43
  • try to ``` [JsonProperty("avgPx")] public string avgPx { get; set; } = null;``` – Bassem Abd Allah Apr 26 '20 at 19:49
  • try to ``` [JsonProperty("avgPx")] public string avgPx { get; set; } = null;``` – Bassem Abd Allah Apr 26 '20 at 19:49

1 Answers1

0

The error occurs because you are trying deserializing a JSON object to a JSON array. So you should provide a JSON array as in input. For the given JSON, add brackets [ ] to the first and last of the JSON string for creating valid JSON array:

var jsonString = "[{\"orderID\":\"8d853505-248d-e515-ee17-ddcd24b5fecb\",\"account\":1024235,\"symbol\":\"XBTUSD\",\"timestamp\":\"2020-04-20T18:25:07.601Z\",\"clOrdID\":\"\",\"side\":\"Buy\",\"price\":6885.5,\"orderQty\":8700,\"ordStatus\":\"Filled\",\"text\":\"ZT\",\"transactTime\":\"2020-04-20T18:22:11.135Z\",\"avgPx\":6885.5}]"
Hadi Samadzad
  • 1,480
  • 2
  • 13
  • 22