2

サーバーダッシュボードアプリを構築しています。各サーバーからディスクのリストを取得し、それぞれの使用量の値を表示するリストを作成したいと考えています。

これが返される JSON サンプルです...

    {"server":"webster","disks":[ {"use": "91%", "used": "16G", "mount": "/", "free": "1.6G", "device": "/dev/mapper/vg_f12-lv_root", "total": "18G", "type": "ext4"} ,
{"use": "0%", "used": "0", "mount": "/dev/shm", "free": "500M", "device": "tmpfs", "total": "500M", "type": "tmpfs"} ,
{"use": "22%", "used": "40M", "mount": "/boot", "free": "145M", "device": "/dev/sda1", "total": "194M", "type": "ext4"} ,
{"use": "47%", "used": "52G", "mount": "/rsync", "free": "61G", "device": "/dev/sdb1", "total": "119G", "type": "ext3"} ]}

私はC#コードでこれまでに取得します:

            WebClient c = new WebClient();
            var data = c.DownloadString("http://192.0.0.40:8000/cgi-bin/df.py");
            JObject o = JObject.Parse(data);
            string serv = o["server"].Select(s => (string)s).ToString();
            lblJson.Text = serv;

しかし、リストビューにプラグインできる意味のあるものに「ディスク」を抽出できないようです。これを IList に送り込もうとしましたが、常にクラッシュするか、Intellisense から無礼なコメントが返ってきます。

このために構築されたクラスがありますが、情報を移植する方法がわかりません。参考までに、それはここにあります:

public class drive
    {
        public string Usage;
        public string usedSpace;
        public string Mount;
        public string freeSpace;
        public string Device;
        public string Total;
        public string Type;
    }

注: JSON のソースは Linux サーバーです。Windows サーバーは、最終的に別の形式でデータを提供します。

それから VMWare がありますが、それについては後で苦労します。

前もって感謝します。

4

2 に答える 2

4
var jsonObj = JsonConvert.DeserializeObject<RootObject>(json);


public class RootObject
{
    [JsonProperty("server")]
    public string Server;
    [JsonProperty("disks")]
    public List<Drive> Disks;
}

public class Drive
{
    [JsonProperty("use")]
    public string Usage;
    [JsonProperty("used")]
    public string usedSpace;
    [JsonProperty("mount")]
    public string Mount;
    [JsonProperty("free")]
    public string freeSpace;
    [JsonProperty("device")]
    public string Device;
    [JsonProperty("total")]
    public string Total;
    [JsonProperty("type")]
    public string Type;
}
于 2012-11-26T19:31:56.343 に答える
2

これを行うためのより良い方法があるかもしれませんが、提供されたdriveクラスを使用して、提供された JSON を逆シリアル化するために次のように機能します。

JObject o = JObject.Parse(data);
List<drive> drives = new List<drive>();
string server = (string)o["server"];
foreach (var d in o["disks"].Children())
{
    drives.Add(new drive()
    {
        Usage = (string)d["use"],
        usedSpace = (string)d["used"],
        Mount = (string)d["mount"],
        freeSpace = (string)d["free"],
        Device = (string)d["device"],
        Total = (string)d["total"],
        Type = (string)d["type"]
    });
}
于 2012-11-26T19:30:01.040 に答える