Deserialization of JSON

Asked

Viewed 272 times

3

I’m having a hard time deserializing this JSON:

  string json = "{\"Cidade\":[\"Arrio do Sal\",\"Atl\\u00e2ntida\",\"Bom Princ\\u00edpio\",\"Brochier\",\"Cachoeirinha\",\"Canaos\",\"Canela\",\"Canoas\",\"Cap\\u00e3o da Canoa\",\"Cidreira\",\"Distrito Morungava\",\"Eldorado Do Sul\",\"Esteio\",\"Florian\\u00f3polis\",\"Governador Celso De Ramos\",\"Gramado\",\"Gravata\\u00ed\",\"Gua\\u00edba\",\"Imb\\u00e9\",\"Ivoti\",\"Montenegro\",\"Nova Petr\\u00f3polis\",\"Nova Santa Rita\",\"Nova Tramanda\\u00ed\",\"Novo Hamburgo\",\"Os\\u00f3rio\",\"Pinhal\",\"Porto Alegre\",\"S\\u00e3o Francisco De Paula\",\"S\\u00e3o jos\\u00e9 do Herval\",\"S\\u00e3o Leopoldo\",\"Sapucaia Do Sul\",\"Terra De Areia\",\"Torres\",\"Tramanda\\u00ed\",\"Triunfo\",\"Viam\\u00e3o\"]}";

I have tried several ways using the Newtonsoft library.

I created a class Cidade to deserialize on an object, I created a class Cidadeitem which is a List of class objects Cidade, I tried to deserialize in a List<String>, tried with Dictionary, all without success. The error reported is this:

Newtonsoft.Json.Jsonserializationexception: 'Cannot deserialize the Current JSON Object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Apivistaconsole.Cidadeitens]' 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 'City', line 1, position 10.'

I put the JSON return right here to facilitate, but I can put the method I use to call the REST if it will help clarify. I’ve been able to deserialize other more complex JSON structures, but this string-only array isn’t really working.

  • in fact your class would need to own a property string[] Cidade. Include your model and the code you’re using for the deserialization.

  • Welcome to Stack Overflow! For programming issues, the best is put the code you’ve already made (instead of just describing it), as this makes it easier for everyone who will try to answer your question. Better understand how the site works by reading the [tour] and the page [Ask]. If the code has gotten too big, you can try to reduce it to a [mcve] <- read this link, there are cool tips on how to post the code here. Also don’t forget to post the code as text (and not as image, understand the reason why reading here)

  • And also see formatting tips in the help center (here and here) and in the FAQ.

3 answers

1

Simple, your object has a list of cities, so it looks like this.

using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = "{\"Cidade\":[\"Arrio do Sal\",\"Atl\\u00e2ntida\",\"Bom Princ\\u00edpio\",\"Brochier\",\"Cachoeirinha\",\"Canaos\",\"Canela\",\"Canoas\",\"Cap\\u00e3o da Canoa\",\"Cidreira\",\"Distrito Morungava\",\"Eldorado Do Sul\",\"Esteio\",\"Florian\\u00f3polis\",\"Governador Celso De Ramos\",\"Gramado\",\"Gravata\\u00ed\",\"Gua\\u00edba\",\"Imb\\u00e9\",\"Ivoti\",\"Montenegro\",\"Nova Petr\\u00f3polis\",\"Nova Santa Rita\",\"Nova Tramanda\\u00ed\",\"Novo Hamburgo\",\"Os\\u00f3rio\",\"Pinhal\",\"Porto Alegre\",\"S\\u00e3o Francisco De Paula\",\"S\\u00e3o jos\\u00e9 do Herval\",\"S\\u00e3o Leopoldo\",\"Sapucaia Do Sul\",\"Terra De Areia\",\"Torres\",\"Tramanda\\u00ed\",\"Triunfo\",\"Viam\\u00e3o\"]}";
            RootObject rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);

            var teste = rootObject.Cidade;
        }
    }


    public class RootObject
    {
        public List<string> Cidade { get; set; }
    }
}

1

The example you showed does not represent an object Cidade who owns a property CidadeItem of the kind List<string>.

It is actually an object that owns a property Cidade which can be a Ienumerable or even a string[].

class ExemploModel
{
    public IEnumerable<string> Cidade { get; set; }
}

Now yes deserialize will work:

string json = "{\"Cidade\":[\"Arrio do Sal\",\"Atl\\u00e2ntida\",\"Bom Princ\\u00edpio\",\"Brochier\"]}"; //...

var listaCidades = JsonConvert.DeserializeObject<ExemploModel>(json);

1

I did in cosoleApplication and it worked

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stack
{
class Program
{
    public List<string> Cidade { get; set; }
    static void Main(string[] args)
    {
        string json = "{\"Cidade\":[\"Arrio do Sal\",\"Atl\\u00e2ntida\",\"Bom Princ\\u00edpio\",\"Brochier\",\"Cachoeirinha\",\"Canaos\",\"Canela\",\"Canoas\",\"Cap\\u00e3o da Canoa\",\"Cidreira\",\"Distrito Morungava\",\"Eldorado Do Sul\",\"Esteio\",\"Florian\\u00f3polis\",\"Governador Celso De Ramos\",\"Gramado\",\"Gravata\\u00ed\",\"Gua\\u00edba\",\"Imb\\u00e9\",\"Ivoti\",\"Montenegro\",\"Nova Petr\\u00f3polis\",\"Nova Santa Rita\",\"Nova Tramanda\\u00ed\",\"Novo Hamburgo\",\"Os\\u00f3rio\",\"Pinhal\",\"Porto Alegre\",\"S\\u00e3o Francisco De Paula\",\"S\\u00e3o jos\\u00e9 do Herval\",\"S\\u00e3o Leopoldo\",\"Sapucaia Do Sul\",\"Terra De Areia\",\"Torres\",\"Tramanda\\u00ed\",\"Triunfo\",\"Viam\\u00e3o\"]}";

        Cidade cidades = JsonConvert.DeserializeObject<Cidade>(json);
    }
}
}
  • You have created a City class or just the property?

  • For the test I created only the property. )

Browser other questions tagged

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