Deserialization of JSON

3

I'm having difficulty 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 in many ways using the NewtonSoft library.

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

  

Newtonsoft.Json.JsonSerializationException: 'Can not deserialize the   current JSON object (e.g. {"name": "value"}) into type   'System.Collections.Generic.List'1 [ApiVistaConsole.CityItems]'   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 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 for ease, but I can put the method I use to call REST if it will help clarify. I've already been able to deserialize other, more complex JSON structures, but this array of strings is just not really working.

    
asked by anonymous 16.11.2018 / 17:09

3 answers

1

Simple, your object has a list of cities, so stay put.

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; }
    }
}
    
16.11.2018 / 17:28
1

The example you showed does not represent a Cidade object that has a CidadeItem property of type List<string> .

It is actually an object that has a Cidade q property that can be an IEnumerable or a string [].

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

Now the deserialize will work:

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

var listaCidades = JsonConvert.DeserializeObject<ExemploModel>(json);
    
16.11.2018 / 17:29
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);
    }
}
}
    
16.11.2018 / 17:31