反序列化json数据c#

时间:2016-08-30 08:35:44

标签: c# json telegram-bot

我想通过Telegram Bot API方法反序列化getUpdate()收到的JSON响应。

JSON数据:

{
  "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
          }
        ]
      }
    }
  ]
}

我已经从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; }
}

使用newtonsoft进行反序列化

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

接下来该做什么?如何把这一切都付诸实践?我很困惑请帮忙。

4 个答案:

答案 0 :(得分:4)

要反序列化,请使用以下代码:

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

ro中,您拥有所有数据。

示例

bool ok = ro.ok;

foreach(Result r in ro.result)
{
    int uId = r.update_id;
    Message m = r.message;
    int msgId = m.message_id;
}

答案 1 :(得分: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);
}

答案 2 :(得分:1)

基于:https://yts.am/api/v2/list_movies.json(来源:https://yts.am/api

我曾经使用:http://json2csharp.com/创建课程 Copy and paste the result of the get request of postman to json2csharp to automatically create the classes that you need

一旦创建,我就在这样的服务上使用它们:

 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;
    }

我正在使用Newtonsoft.Json

答案 3 :(得分:0)

试试这种方式

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

然后d将包含RootObject的列表。通过使用foreach进行迭代,您可以获得RootObject模型

的所有属性