Answer the question
In order to leave comments, you need to log in
C#. How to parse JSON correctly?
Good afternoon!
I'm trying to parse the Json response from the API into classes, etc.
Generated classes on jsonutils.com
Copied to the project, registered:
var fb = new WebClient() {Encoding = System.Text.Encoding.UTF8}.DownloadString(URI);
var list = JsonConvert.DeserializeObject<List<Example>>(fb);
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[chat.Example]' 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<T>) 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.
Answer the question
In order to leave comments, you need to log in
At you in an error it is written - you try to turn an object into an array. Your classes are generated incorrectly, look for an error in them
There is a great service for generating classes from JSON: https://app.quicktype.io/#r=json2csharp
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question