0

I have JSON string, Its giving error when I am trying to deserialize it.

JSON:

[
    [
        {
            "fieldNameFK": 5
           
        }
    ],
    [
        {
            "fieldNameFK": 6
            
        }
    ]
]

Class:

public class Root
{
    public List<List<Test>> Test{ get; set; }
}

public class Test
{
  
    public int FieldNameFK { get; set; }

}

Code to deserialize

var response = JsonConvert.DeserializeObject<Root>(jsonStr);

Exception:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ConsoleApp1.Program+Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To 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. Path '', line 1, position 1.

I have referred this and this solution also, but It didn't help.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197

1 Answers1

3

You have an json array at your root, not an json object so you need to deserialize to a collection, not object:

var response = JsonConvert.DeserializeObject<List<List<Test>>>(jsonStr);
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Guru Stron
  • 102,774
  • 10
  • 95
  • 132