Это для небольшого школьного проекта. Я пытаюсь превратить JSON в класс, к которому я затем смогу получить доступ.
Это пример вывода json для данного слова «привет»:
https://api.dictionaryapi.dev/ api/v2/entries/en/hello
А вот мой код для его десериализации:
Код: Выделить всё
public class Word
{
public string word;
public string phonetic;
public string origin;
public phoneticitem[] phonetics;
public meaning[] meanings;
public sealed class phoneticitem
{
public string text;
public string audio;
public string source;
public license license;
}
public sealed class license
{
public string name;
public string url;
}
public sealed class meaning
{
public string partOfSpeech;
public definitionEntry[] definitions;
}
public sealed class definitionEntry
{
public string definition;
public string[] synonyms;
public string[] antonyms;
public string example;
}
}
Код: Выделить всё
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'DictionaryAPI.Word' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
at DictionaryAPI.Dictionary.FindWord(String word, String language) in C:\Users\Awire\source\repos\DictionaryDiscordBot\DictionaryDiscordBot\Dictionary.cs:line 20
at DictionaryBot.Commands.define(String word) in C:\Users\Awire\source\repos\DictionaryDiscordBot\DictionaryDiscordBot\Commands.cs:line 29
at Discord.Interactions.Builders.ModuleClassBuilder.c__DisplayClass11_0.d.MoveNext()
Что-то мне не хватает?
Пытался сделать разные вещи общедоступными, но безрезультатно .
Подробнее здесь: https://stackoverflow.com/questions/784 ... rray-at-it
Мобильная версия