0

I keep getting an error after running the code for a few minutes

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[BitMEX.Order]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3])

Can you please help me?

public List<Position> GetOpenPositions(string symbol)
{
    var param = new Dictionary<string, string>();

    string res = Query("GET", "/position", param, true);

    return JsonConvert.DeserializeObject<List<Position>>(res)
        .Where(a => a.Symbol == symbol && a.IsOpen == true)
        .OrderByDescending(a => a.TimeStamp)
        .ToList();
}
PoLáKoSz
  • 355
  • 1
  • 6
  • 7
jcjurevis
  • 1
  • 2
  • You are getting this error because you are getting back an Object rather than an array, to check it try this `JsonConvert.DeserializeObject(res).Where(a => a.Symbol == symbol && a.IsOpen == true).OrderByDescending(a => a.TimeStamp)` – progrAmmar Jan 16 '18 at 03:15
  • why you are doing `.ToList()` to your result? try removing it – Joker_37 Jan 16 '18 at 03:19
  • @progrAmmar I suggest you to make your comment an answer, because it looks correct, and no one will answer it anymore. But this question will be marked as unanswered. – Alex Butenko Jan 16 '18 at 04:05
  • I tried removing .ToList() and it returns another error. – jcjurevis Jan 16 '18 at 04:18
  • return JsonConvert.DeserializeObject(res) .Where(a => a.Symbol == symbol && a.IsOpen == true) .OrderByDescending(a => a.TimeStamp); Can you try this? – Hameed Syed Jan 16 '18 at 04:31
  • @jcjurevis - somewhere in your data model you have a `List` property that should just be a `BitMEX.Order` property. Without your c# classes or your JSON that's all we can say, so this should be closed as a duplicate of [Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1](https://stackoverflow.com/q/21358493/3744182) and [Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class](https://stackoverflow.com/q/11126242/3744182). – dbc Jan 16 '18 at 04:35

0 Answers0