skip to Main Content

I want to deserialize JSON response received by Telegram Bot API by getUpdate() method.

JSON data:

{
  "ok": true,
  "result": [
    {
      "update_id": 920493886,
      "message": {
        "message_id": 123,
        "from": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777"
        },
        "chat": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777",
          "type": "private"
        },
        "date": 1472457375,
        "text": "Aata aala"
      }
    },
    {
      "update_id": 920493887,
      "message": {
        "message_id": 124,
        "from": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777"
        },
        "chat": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777",
          "type": "private"
        },
        "date": 1472457387,
        "text": "Jeva tuzyakadun reply aala tevha"
      }
    },
    {
      "update_id": 920493888,
      "message": {
        "message_id": 125,
        "from": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777"
        },
        "chat": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777",
          "type": "private"
        },
        "date": 1472457443,
        "text": "Deposite"
      }
    },
    {
      "update_id": 920493889,
      "message": {
        "message_id": 127,
        "from": {
          "id": 201520743,
          "first_name": "Chandrakant",
          "last_name": "Kumathekar",
          "username": "chandrakant_k"
        },
        "chat": {
          "id": 201520743,
          "first_name": "Chandrakant",
          "last_name": "Kumathekar",
          "username": "chandrakant_k",
          "type": "private"
        },
        "date": 1472457645,
        "text": "/menu",
        "entities": [
          {
            "type": "bot_command",
            "offset": 0,
            "length": 5
          }
        ]
      }
    },
    {
      "update_id": 920493890,
      "message": {
        "message_id": 128,
        "from": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777"
        },
        "chat": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777",
          "type": "private"
        },
        "date": 1472457670,
        "text": "/menu",
        "entities": [
          {
            "type": "bot_command",
            "offset": 0,
            "length": 5
          }
        ]
      }
    },
    {
      "update_id": 920493891,
      "message": {
        "message_id": 130,
        "from": {
          "id": 201520743,
          "first_name": "Chandrakant",
          "last_name": "Kumathekar",
          "username": "chandrakant_k"
        },
        "chat": {
          "id": 201520743,
          "first_name": "Chandrakant",
          "last_name": "Kumathekar",
          "username": "chandrakant_k",
          "type": "private"
        },
        "date": 1472457848,
        "text": "Deposite"
      }
    },
    {
      "update_id": 920493892,
      "message": {
        "message_id": 132,
        "from": {
          "id": 201520743,
          "first_name": "Chandrakant",
          "last_name": "Kumathekar",
          "username": "chandrakant_k"
        },
        "chat": {
          "id": 201520743,
          "first_name": "Chandrakant",
          "last_name": "Kumathekar",
          "username": "chandrakant_k",
          "type": "private"
        },
        "date": 1472457883,
        "text": "Deposite"
      }
    },
    {
      "update_id": 920493893,
      "message": {
        "message_id": 133,
        "from": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777"
        },
        "chat": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777",
          "type": "private"
        },
        "date": 1472468407,
        "text": "/menu",
        "entities": [
          {
            "type": "bot_command",
            "offset": 0,
            "length": 5
          }
        ]
      }
    },
    {
      "update_id": 920493894,
      "message": {
        "message_id": 134,
        "from": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777"
        },
        "chat": {
          "id": 219633031,
          "first_name": "Shreeeeeee",
          "username": "Winner7777",
          "type": "private"
        },
        "date": 1472473070,
        "text": "/menu",
        "entities": [
          {
            "type": "bot_command",
            "offset": 0,
            "length": 5
          }
        ]
      }
    }
  ]
}

I have generated classes from json2csharp

public class From
{
    public int id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string username { get; set; }
}

public class Chat
{
    public int id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string type { get; set; }
    public string username { get; set; }
}

public class Entity
{
    public string type { get; set; }
    public int offset { get; set; }
    public int length { 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 RootObject
{
    public bool ok { get; set; }
    public List<Result> result { get; set; }
}

using newtonsoft to deserialization

var d = Newtonsoft.Json.JsonConvert.DeserializeObject(rcvd_data);

what to do next? how to put this all at work? I am confused please help.

4

Answers


  1. To deserialization use this code:

    RootObject ro = JsonConvert.DeserializeObject<RootObject>(rcvd_data);
    

    And in ro you have all data.

    EXAMPLE

    bool ok = ro.ok;
    
    foreach(Result r in ro.result)
    {
        int uId = r.update_id;
        Message m = r.message;
        int msgId = m.message_id;
    }
    
    Login or Signup to reply.
  2. Try this way

    var d = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(rcvd_‌​data);
    

    then d will contain list of RootObject. By iterating using foreach you can get all the properties of RootObject model

    Login or Signup to reply.
  3. string data = @"{""ok"":true,""result"":[{""update_id"":920493886,...";
    
    RootObject ro = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(data);
    
    foreach (Result result in ro.result)
    {
            //two example fields
            Console.WriteLine("update_id= " + result.update_id);
            Console.WriteLine("message text= "+result.message.text);
    }
    
    Login or Signup to reply.
  4. Based on: https://yts.am/api/v2/list_movies.json (Source: https://yts.am/api)

    I used: http://json2csharp.com/ to create classes
    Copy and paste the result of the get request of postman to json2csharp to automatically create the classes that you need

    And once created, i used them on a Service like this:

     private string URL = "https://yts.am/api/v2/list_movies.json";
        public async Task<List<Movie>> GetMovies()
        {
            var httpClient = new HttpClient();
            var json = await httpClient.GetStringAsync(URL);
            RootObject lista = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
            List<Movie> todoes = lista.data.movies;
            return todoes;
        }
    

    I’m using Newtonsoft.Json

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search