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.

Oliver
  • 43,366
  • 8
  • 94
  • 151
ck jon
  • 31
  • 2
  • 1
    Specify a type when you use DeserializeObject, like so: `Message message = Newtonsoft.Json.JsonConvert.DeserializeObject(rcvd_data);`. And then you can retrieve the Message properties (`message.text`). – Bryan Woodford Aug 30 '16 at 08:46
  • 2
    this question was asked ~500 times and shows no research effort! – Matthias Burger Aug 30 '16 at 09:03
  • 1
    Possible duplicate of [Deserializing JSON data to C# using JSON.NET](http://stackoverflow.com/questions/2546138/deserializing-json-data-to-c-sharp-using-json-net) – Matthias Burger Aug 30 '16 at 09:25

4 Answers4

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;
}
BWA
  • 5,672
  • 7
  • 34
  • 45
  • 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?? – ck jon Aug 30 '16 at 09:17
  • What values have you null? I check it – BWA Aug 30 '16 at 09:18
  • Result Result = JsonConvert.DeserializeObject(rcvd_data); int id = Result.update_id; – ck jon Aug 30 '16 at 09:18
  • Message message = Newtonsoft.Json.JsonConvert.DeserializeObject(rcvd_data); string msgid = message.message_id.ToString(); string msgtxt = message.text; Chat cht = Newtonsoft.Json.JsonConvert.DeserializeObject(rcvd_data); int cht_id = cht.id; string frst_nm = cht.first_name; – ck jon Aug 30 '16 at 09:20
  • also this object i am getting null values – ck jon Aug 30 '16 at 09:21
  • @ckjon dont deserialize to `Result`, deserialize to `RootObject`. Then in ro you get list of Result. – BWA Aug 30 '16 at 09:22
  • @yes, i udpated my answer – BWA Aug 30 '16 at 09:24
  • Thank you so much @BWA it was really helpful. – ck jon Aug 30 '16 at 09:31
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);
}
robor
  • 2,969
  • 2
  • 31
  • 48
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

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

Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • 1
    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. – Lars Kristensen Aug 30 '16 at 08:53
  • yes first time I do mistake but unfortunately during edit my power was gone so can't edit, now ok – Mostafiz Aug 30 '16 at 09:09