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.

1
  • are you looking to Deserialize the JSON script..? Commented Nov 26, 2012 at 19:21

2 Answers 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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

OP can use this StackOverFlow previous post for simple example of how to Parse JSON / Deserializing JSON stackoverflow.com/questions/1212344/parse-json-in-c-sharp
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"]
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.