JSON не хочет парситься в класс! C#

131
20 июня 2019, 17:50

Делаю бота Телеграмм. При парсинге через Newtonsoft.json выдает ошибку. Вот все использованные материалы. JSON данные:

{
    "ok": true,
    "result": [{
            "update_id": 787055778,
            "message": {
                "message_id": 1,
                "from": {
                    "id": 420474275,
                    "is_bot": false,
                    "first_name": "Name;",
                    "username": "User"
                },
                "chat": {
                    "id": 420474275,
                    "first_name": "Name;",
                    "username": "User",
                    "type": "private"
                },
                "date": 1544978929,
                "text": "/start",
                "entities": [{
                        "offset": 0,
                        "length": 6,
                        "type": "bot_command"
                    }
                ]
            }
        }
    ]
}

Код парсинга:

        public static void GetUpdates()
    {
        using (WebClient webClient = new WebClient())
        {
            string update = webClient.DownloadString($"https://api.telegram.org/bot{Token}/getUpdates");
            var parsedUpdate = JsonConvert.DeserializeObject<Data>(update);
        };
    }
public class Data
{
    [JsonProperty("result")]
    public Dictionary<string, Result> Result { get; set; }
}
public class Result
{
    [JsonProperty("0")]
    public Dictionary<string, Zero1> Zero1 { get; set; }
}
public class Zero1
{
    [JsonProperty("update_id")]
    public string UpdateID { get; set; }
    [JsonProperty("message")]
    public Dictionary<string, Message> Message { get; set; }
}
public class Message
{
    [JsonProperty("message_id")]
    public string MessageID { get; set; }
    [JsonProperty("from")]
    public Dictionary<string, From> From { get; set; }
    [JsonProperty("text")]
    public string Text { get; set; }
    [JsonProperty("entities")]
    public Dictionary<string, Entities> Entities { get; set; }
}
public class From
{
    [JsonProperty("is_bot")]
    public string IsBot { get; set; }
    [JsonProperty("first_name")]
    public string FirstName { get; set; }
}
public class Entities
{
    [JsonProperty("0")]
    public Dictionary<string, Zero2> Zero2;
}
public class Zero2
{
    [JsonProperty("type")]
    public string Type { get; set; }
}

Вот ошибка:

Newtonsoft.Json.JsonSerializationException: "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Бот_викторина_1.Result]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Answer 1

Не правильно сформировали код.

var parsedUpdate = JsonConvert.DeserializeObject<Data>(update);
public class From
{
    public int id { get; set; }
    public bool is_bot { get; set; }
    public string first_name { get; set; }
    public string username { get; set; }
}
public class Chat
{
    public int id { get; set; }
    public string first_name { get; set; }
    public string username { get; set; }
    public string type { get; set; }
}
public class Entity
{
    public int offset { get; set; }
    public int length { get; set; }
    public string type { get; set; }
}
public class Message
{
    public int message_id { get; set; }
    public From from { get; set; }
    public Chat chat { get; set; }
    public int date { get; set; }
    public string text { get; set; }
    public List<Entity> entities { get; set; }
}
public class Result
{
    public int update_id { get; set; }
    public Message message { get; set; }
}
public class Data
{
    public bool ok { get; set; }
    public List<Result> result { get; set; }
}
READ ALSO
Как в коде определить что свойство объекта является итерируемым?

Как в коде определить что свойство объекта является итерируемым?

Хочу сделать универсальный метод выгружающий таблицу в pdf

147
Unity определение угла между 2мя векторами

Unity определение угла между 2мя векторами

Я хочу узнать как вычислить угол между 2мя точками мыши: mousePosx, mousePos

133
Dynamics 365, drag and drop

Dynamics 365, drag and drop

У меня на форме Dynamics 365 есть веб-ресурс HTML страница с зоной Drag and drop, как можно перетащенный файл загрузить в SharePoint?

131