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 aCidade
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.