T
T
TechNOIR2018-03-07 12:19:57
C++ / C#
TechNOIR, 2018-03-07 12:19:57

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);

I get the following:
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.

Please tell me how to solve the problem? Thanks in advance!
JSON - ignn.ru/t/messages.json

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2018-03-07
@byte916

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

V
VoidVolker, 2018-03-07
@VoidVolker

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 question

Ask a Question

731 491 924 answers to any question