Не удаётся неявно преорбазовать тип данных при парсинге json'а c# newsoft.json
Есть такой JSON:
[{
"quoteText": "You can observe a lot just by watching.",
"quoteAuthor": "Yogi Berra"
},
{
"quoteText": "A house divided against itself cannot stand.",
"quoteAuthor": "Abraham Lincoln"
},
{
"quoteText": "Difficulties increase the nearer we get to the goal.",
"quoteAuthor": "Johann Wolfgang von Goethe"
}]
Для него созданы 2 класса Rootobject и Class1:
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string quoteText { get; set; }
public string quoteAuthor { get; set; }
}
Пытаюсь его десериализировать вот так:
Quotes = JsonConvert.DeserializeObject<List<Rootobject>>(Quotes_data);
Но всё равно выдаёт ошибку:
Не удается неявно преобразовать тип "System.Collections.Generic.List<ConsoleApp1.Rootobject>" в "ConsoleApp1.Rootobject"
Ответы (2 шт):
Автор решения: yungstepik
→ Ссылка
Помогло сделать так:
static public List<Dictionary<string, string>> Quotes;
Quotes = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(Quotes_data);
Автор решения: laRaptor
→ Ссылка
Мне кажется вам не нужен объект RootObject. У вас в JSON записан массив объектов, а не объект с массивом внутри.
Если попробовать вот так, все получается
public class Class1
{
public string quoteText { get; set; }
public string quoteAuthor { get; set; }
}
class Program
{
static void Main(string[] args)
{
string jsonObj = "[{\"quoteText\":\"You can observe a lot just by watching.\",\"quoteAuthor\":\"Yogi Berra\"},{\"quoteText\":\"A house divided against itself cannot stand.\",\"quoteAuthor\":\"Abraham Lincoln\"},{\"quoteText\":\"Difficulties increase the nearer we get to the goal.\",\"quoteAuthor\":\"Johann Wolfgang von Goethe\"}]";
var deserializedObjec = JsonConvert.DeserializeObject<List<Class1>>(jsonObj);
}
}