2

I am building a server dashboard app. I want to take a list of disks from each server and create a list that displays the usage values for each.

Here's a JSON sample we're getting back...

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

I get this far with the C# code:

            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;

But I can't seem to extract "disks" into anything meaningful that I can plugin to a listview. I've tried pumping this into IList, but it always crashes or gives me some rude comments from Intellisense.

I do have a class built for this, but haven't figured out how to port the info into it. For reference, it's here:

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

Note: The sources for JSON are Linux servers. Windows servers will supply data in a different format ultimately.

And then we have VMWare, but I'll flail on that later.

Thanks in advance.

Tom
  • 1,105
  • 8
  • 15

2 Answers2

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;
}
L.B
  • 114,136
  • 19
  • 178
  • 224
  • OP can use this StackOverFlow previous post for simple example of how to Parse JSON / Deserializing JSON http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp – MethodMan Nov 26 '12 at 19:33
2

There may be a better way to do this, but using the provided drive class, the following works to deserialize your provided 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"]
    });
}
Adam S
  • 16,144
  • 6
  • 54
  • 81