如何修剪字符串的部分以获取所需信息?

时间:2014-02-28 22:28:16

标签: c# json

我想在文本框中显示每个内容:

{"hash": "somehashencryption", "ip": "this.is.the.ip", "maxplayers": 64, "mppass": "3e6876ca64423fc478073f511dc7e11a", "name": "Name of the server", "players": 0, "port": 25565, "uptime": 2050648}

以这种形式:

name = Name of the server

ip = this.is.the.ip

mppass = 3e6876ca64423fc478073f511dc7e11a

port = 25565

uptime = 2050648

hash = somehashencryption

我在编码方面非常基础,我正在尝试将其转换为列表,有一个API:

http://www.classicube.net/api/serverlist/

1 个答案:

答案 0 :(得分:0)

  1. 使用NuGet在项目中包含JSON.NET。
  2. 使用http://json2csharp.com/将您的示例输入转换为如下类:

    public class ServerListItem
    {
        public string hash { get; set; }
        public string ip { get; set; }
        public int maxplayers { get; set; }
        public string mppass { get; set; }
        public string name { get; set; }
        public int players { get; set; }
        public int port { get; set; }
        public int uptime { get; set; }
    }
    
  3. 反序列化并使用如下数据:

    var s = "{\"hash\": \"somehashencryption\", \"ip\": \"this.is.the.ip\", \"maxplayers\": 64, \"mppass\"" +
        ": \"3e6876ca64423fc478073f511dc7e11a\", \"name\": \"Name of the server\", \"players\": 0" +
        ", \"port\": 25565, \"uptime\": 2050648}";
    
    var server = JsonConvert.DeserializeObject<ServerListItem>(s);
    Console.WriteLine("Hash: " + server.hash);