user142617 user142617 - 23 days ago 5
C# Question

c# Newtonsoft.Json DeserializeObject error

I am trying to deserialise the following json object but getting an error, all I would like to do is put the json into a c# List:

public class CardTextModel
{
public string prod_Code { get; set; }
public string page1Text { get; set; }
public string insideText { get; set; }
public string userName { get; set; }
public Nullable<System.DateTime> exportDate { get; set; }
public List<CardTextModel> card_Text { get; set; }
}


Here is where I try to deserialise the json

class Program
{
static void Main(string[] args)
{
try
{
var content = @"{
""card_Text"": [
{
""prod_Code"": ""G01Q0320WS"",
""page1Text"": ""SHORTY SET SZ 10"",
""insideText"": ""SHORTY SET SZ 10"",
""userName"": ""daz"",
""exportDate"": null
},
{
""prod_Code"": ""G01Q0380"",
""page1Text"": ""TREE DECS SET 4 RESIN"",
""insideText"": ""TREE DECS SET 4 RESIN"",
""userName"": ""mark"",
""exportDate"": null
}
]
}";

var model = JsonConvert.DeserializeObject<List<CardTextModel>>(content);
}
catch (Exception ex)
{

}
}
}


I am getting the following error when I try to deserize my json object


Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CardTextModel]' 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]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'card_Text', line 2, position 30.


I am using Newtonsoft.Json 9.0.0.0

Thanks for any help

Answer

Your json should look like:

var content = @"[
                {
                  ""prod_Code"": ""G01Q0320WS"",
                  ""page1Text"": ""SHORTY SET SZ 10"",
                  ""insideText"": ""SHORTY SET SZ 10"",
                  ""userName"": ""daz"",
                  ""exportDate"": null
                },
                {
                  ""prod_Code"": ""G01Q0380"",
                  ""page1Text"": ""TREE DECS SET 4 RESIN"",
                  ""insideText"": ""TREE DECS SET 4 RESIN"",
                  ""userName"": ""mark"",
                  ""exportDate"": null
                }
              ]";

var model = JsonConvert.DeserializeObject<List<CardTextModel>>(content);

Or your object needs to change to support the root element card_Text.

public class CardTextModel
{
    public string prod_Code { get; set; }
    public string page1Text { get; set; }
    public string insideText { get; set; }
    public string userName { get; set; }
    public Nullable<System.DateTime> exportDate { get; set; }
    public List<CardTextModel> card_Text { get; set; }
}

public class CardTextModelRoot
{
    public List<CardTextModel> card_Text {get;set;}
}

var content = @"{
      ""card_Text"": [
        {
          ""prod_Code"": ""G01Q0320WS"",
          ""page1Text"": ""SHORTY SET SZ 10"",
          ""insideText"": ""SHORTY SET SZ 10"",
          ""userName"": ""daz"",
          ""exportDate"": null
        },
        {
          ""prod_Code"": ""G01Q0380"",
          ""page1Text"": ""TREE DECS SET 4 RESIN"",
          ""insideText"": ""TREE DECS SET 4 RESIN"",
          ""userName"": ""mark"",
          ""exportDate"": null
        }
      ]
    }";

var model = JsonConvert.DeserializeObject<CardTextModelRoot>(content);