2

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.

3
  • 1
    Specify a type when you use DeserializeObject, like so: Message message = Newtonsoft.Json.JsonConvert.DeserializeObject<Message>(rcvd_data);. And then you can retrieve the Message properties (message.text). Commented Aug 30, 2016 at 8:46
  • 2
    this question was asked ~500 times and shows no research effort! Commented Aug 30, 2016 at 9:03
  • 1
    Possible duplicate of Deserializing JSON data to C# using JSON.NET Commented Aug 30, 2016 at 9:25

4 Answers 4

4

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

thanks for quick reply i did as you said everything goes okk but i get some values null as i compile it. is i missing something??
What values have you null? I check it
Result Result = JsonConvert.DeserializeObject<Result>(rcvd_data); int id = Result.update_id;
Message message = Newtonsoft.Json.JsonConvert.DeserializeObject<Message>(rcvd_data); string msgid = message.message_id.ToString(); string msgtxt = message.text; Chat cht = Newtonsoft.Json.JsonConvert.DeserializeObject<Chat>(rcvd_data); int cht_id = cht.id; string frst_nm = cht.first_name;
also this object i am getting null values
|
1
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);
}

Comments

1

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

Comments

0

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

2 Comments

Judging by the Json data, it should be deserialized to the RootObject class instead. From that object, you can access a list of Message objects.
yes first time I do mistake but unfortunately during edit my power was gone so can't edit, now ok

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.