4

I have an array of objects like this in json as per below format

{
Table: [
     {
      userstatus: [
                   {
                    STATUS: "TRUE",
                    PACK: "UM6MONTHPACK",
                    EXPIRY: "8/15/2014 1:00:03 PM",             
                   }
                  ]
      },

      {
      activeauctions: [
                       {
                         ISBILLED: "0",
                         AUCTION_ID: "24",
                         AUCTION_NAME: "Swimsuit",      
                       }
                      ]
     },

     {
      upcomingauctions: [
                         {
                            AUCTION_ID: "4",
                        AUCTION_NAME: "Jacqueline Fernandezs Handbag",
                            SKU: "4_20131120"
                         },
                         {
                           AUCTION_ID: "4",
                        AUCTION_NAME: "Jacqueline Fernandezs Handbag",
                            SKU: "4_20131120"
                         }
                        ]
      }
  ]
}

I am deserializing like this:

var outObject = JsonConvert.DeserializeObject<Table>(response);

Here are the classes I am deserializing into:

public class Userstatu
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "STATUS")]
    public string STATUS { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "PACK")]
    public string PACK { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "EXPIRY")]
    public string EXPIRY { get; set; }      
}

public class Activeauction
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "ISBILLED")]
    public string ISBILLED { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "AUCTION_ID")]
    public string AUCTION_ID { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "AUCTION_NAME")]
    public string AUCTION_NAME { get; set; }        
}

public class Upcomingauction
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "AUCTION_ID")]
    public string AUCTION_ID { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "AUCTION_NAME")]
    public string AUCTION_NAME { get; set; }
  
    [Newtonsoft.Json.JsonProperty(PropertyName = "SKU")]
    public string SKU { get; set; }
}

public class Table
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "userstatus")]
    public List<Userstatu> userstatus { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "activeauctions")]
    public List<Activeauction> activeauctions { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "upcomingauctions")]
    public List<Upcomingauction> upcomingauctions { get; set; }
}

This fires an exception:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Data.Table]' 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 'accounts.github', line 1, position 129.

What am I doing wrong?

1 Answer 1

5

You are missing a class. Add this:

public class RootObject
{
    public List<Table> Table { get; set; }
}

Then, deserialize like this:

var outObject = JsonConvert.DeserializeObject<RootObject>(response);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.