Я пытаюсь десериализовать JSON, вырежьте его, продолжайте показывать мне это исключение:
Could not cast or convert from System.String to SmartBookLibrary.ViewModel.BookJ1.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Could not cast or convert from System.String to SmartBookLibrary.ViewModel.BookJ1.
Вот пример моего JSON:
{
"authorfamily1": "von Goethe",
"authorname1": "Johann",
"authorsurname1": "Wolfgang",
"title": "Fausto I",
"extension": "epub",
"md5": "58cb1dd438bc6c6027fcda9e7729e5ee",
"isbn": "",
"descr": "",
"cover": "1"
},
{
"authorfamily1": "von Goethe 1",
"authorname1": "Johann",
"authorsurname1": "Wolfgang",
"title": "Fausto I",
"extension": "epub",
"md5": "58cb1dd438bc6c6027fcda9e7729e5ee",
"isbn": "",
"descr": "",
"cover": "1"
}
Вот код:
var json = System.IO.File.ReadAllText("/data1.json");
var courses = JsonConvert.DeserializeObject<Dictionary<string, BookJ1>>(json);
Вот моя модель или виртуальная машина:
public class BookJ1
{
public string title { get; set; }
public string isbn { get; set; }
public string extension { get; set; }
public string authorfamily1 { get; set; }
public string authorname1 { get; set; }
public string md5 { get; set; }
public int cover { get; set; }
[AllowHtml]
[Column(TypeName = "text")]
public string descr { get; set; }
}





Предполагая, что показанный образец таков, как он находится в файле,
вам, скорее всего, нужно отформатировать этот JSON как массив, прежде чем пытаться его десериализовать
var data = System.IO.File.ReadAllText("/data1.json");
var json = string.Format("[{0}]", data);
BookJ1[] courses = JsonConvert.DeserializeObject<BookJ1[]>(json);
Однако, если показанный образец неполный и данные в файле фактически хранятся в виде массива
[{
"authorfamily1": "von Goethe",
"authorname1": "Johann",
"authorsurname1": "Wolfgang",
"title": "Fausto I",
"extension": "epub",
"md5": "58cb1dd438bc6c6027fcda9e7729e5ee",
"isbn": "",
"descr": "",
"cover": "1"
},
{
"authorfamily1": "von Goethe 1",
"authorname1": "Johann",
"authorsurname1": "Wolfgang",
"title": "Fausto I",
"extension": "epub",
"md5": "58cb1dd438bc6c6027fcda9e7729e5ee",
"isbn": "",
"descr": "",
"cover": "1"
}]
тогда вам просто нужно десериализовать правильный тип
var json = System.IO.File.ReadAllText("/data1.json");
BookJ1[] courses = JsonConvert.DeserializeObject<BookJ1[]>(json);
Общий тип аргумента не соответствует показанному JSON. Это полный JSON? Скорее всего, вам нужно сделать этот JSON массивом, прежде чем пытаться его десериализовать.