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.– Leandro Angelo
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)
– hkotsubo
And also see formatting tips in the help center (here and here) and in the FAQ.
– hkotsubo