C# Десерилизовать json
Есть такой json
{
"ChatDb": [
{
"Id": 1,
"ChatId": 86,
"Name": "Чат 90",
"SettingName": null,
"UserSoftId": 1
},
{
"Id": 2,
"ChatId": 86,
"Name": "Чат 91",
"SettingName": null,
"UserSoftId": 1
},
{
"Id": 3,
"ChatId": 86,
"Name": "Чат 93",
"SettingName": null,
"UserSoftId": 1
},
{
"Id": 4,
"ChatId": 86,
"Name": "Чат 94",
"SettingName": null,
"UserSoftId": 1
},
{
"Id": 5,
"ChatId": 86,
"Name": "Чат 95",
"SettingName": null,
"UserSoftId": 1
},
{
"Id": 6,
"ChatId": 14,
"Name": "Чат 100",
"SettingName": null,
"UserSoftId": 1
},
{
"Id": 7,
"ChatId": 14,
"Name": "Чат 101",
"SettingName": null,
"UserSoftId": 1
},
{
"Id": 8,
"ChatId": 14,
"Name": "Чат 102",
"SettingName": null,
"UserSoftId": 1
}
]
}
Код
async void GetChatHome()
{
using var client = new HttpClient();
UserSoft.Token = "Test";
UserSoft.UserID = 1000;
var json = JsonConvert.SerializeObject(UserSoft);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = client.PostAsync("https://localhost:5001/GetChatHome", content).Result;
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
var stt = JsonConvert.DeserializeObject<string>(result);
var chats = JsonConvert.DeserializeObject<Dictionary<string, ChatDb>>(result);
}
else
{
MessageBox.Show($"Не удалось получить чат");
}
}
В строке var stt = JsonConvert.DeserializeObject<string>(result);
ошибка Newtonsoft.Json.JsonReaderException: "Unexpected character encountered while parsing value: {. Path '', line 1, position 1."
Json создаю таким кодом
/// <summary>
/// Получить чаты
/// </summary>
/// <param name="userSoftJson"></param>
/// <returns></returns>
[HttpPost("/GetChatHome")]
public async Task<ActionResult> GetChatHome([FromBody] UserSoft userSoftJson)
{
var user = await db.UserSoft.Include(x => x.ChatDb).FirstOrDefaultAsync(x => x.Token == userSoftJson.Token);
var ChatDb = user.ChatDb.ToList();
if (user == null)
return BadRequest();
Response.ContentType = "application/json; charset=utf-8"; // добавляем указание на кодировку UTF-8
return Content(JsonConvert.SerializeObject(new { ChatDb }), "application/json", Encoding.UTF8); // используем метод Content для отправки JSON с указанием кодировки UTF-8
}
Подскажите, почему стерилизовав в одном проекте, я не могу деселиризовать json в другом проекте.
Источник: Stack Overflow на русском