ERROR DESERIALIZE JSON WITH C#

Asked

Viewed 85 times

-1

I’m getting error System.Invalidoperationexception: 'The 'project.classeCategories' type is not supported for deserialization of an array.' by deserialize JSON below:

See how I’m getting JSON in C#:

var client = new RestClient('https://api.tiendanube.com/v1/1492214/categories');
var request = new RestRequest(Method.GET);
request.AddHeader('Content-Type', 'application/json; charset=utf-8');
request.AddHeader('User-Agent', 'XXXXXX');
request.AddHeader('Authentication', 'XXXXXX');
IRestResponse responseData = client.Execute(request);

JSON (comes in this format when doing GET):

[\n    {\n        \"id\": 8369593,\n        \"parent\": 0,\n        \"subcategories\": [\n            8369664\n        ],\n        \"created_at\": \"2021-02-24T17:28:22+00:00\",\n        \"updated_at\": \"2021-02-24T17:28:22+00:00\",\n        \"name\": {\n            \"pt\": \"Livro\"\n        },\n        \"handle\": {\n            \"pt\": \"livro\"\n        },\n        \"description\": {\n            \"pt\": \"\"\n        },\n        \"seo_title\": {\n            \"pt\": \"\"\n        },\n        \"seo_description\": {\n            \"pt\": \"\"\n        }\n    },\n    {\n        \"id\": 8369664,\n        \"parent\": 8369593,\n        \"subcategories\": [],\n        \"created_at\": \"2021-02-24T17:35:14+00:00\",\n        \"updated_at\": \"2021-02-24T17:35:14+00:00\",\n        \"name\": {\n            \"pt\": \"Administra\\u00e7\\u00e3o\"\n        },\n        \"handle\": {\n            \"pt\": \"administracao\"\n        },\n        \"description\": {\n            \"pt\": \"\"\n        },\n        \"seo_title\": {\n            \"pt\": \"\"\n        },\n        \"seo_description\": {\n            \"pt\": \"\"\n        }\n    }\n]

Below the command to deserialize the JSON (this is where the error occurs):

var JsonConvert = new JavaScriptSerializer();
classeCategorias classeCategoria = JsonConvert.Deserialize<classeCategorias>(responseData.Content.ToString());

I am using the class below (created through the EDIT menu / PASTE SPECIAL / PASTE JSON AS CLASSES in Visual Studio):

public class classeCategorias
    {
         public Class1[] Property1 { get; set; }
    }

    public class Class1
    {
        public int id { get; set; }
        public int parent { get; set; }
        public int?[] subcategories { get; set; }
        public DateTime created_at { get; set; }
        public DateTime updated_at { get; set; }
        public Name name { get; set; }
        public Handle handle { get; set; }
        public Description description { get; set; }
        public Seo_Title seo_title { get; set; }
        public Seo_Description seo_description { get; set; }
    }

    public class Name
    {
        public string pt { get; set; }
    }

    public class Handle
    {
        public string pt { get; set; }
    }

    public class Description
    {
        public string pt { get; set; }
    }

    public class Seo_Title
    {
        public string pt { get; set; }
    }

    public class Seo_Description
    {
        public string pt { get; set; }
    }
  • tries to use a List: public List<Class1> Property1 { get; set; }

  • The same error occurs.

1 answer

0

Hello, thank you all, I managed to resolve (help from another forum). Go to whoever you need:

var JsonConvert = new JavaScriptSerializer();
List<Class1> categorias = JsonConvert.Deserialize<List<Class1>>(responseData.Content.ToString());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.