tags:

views:

236

answers:

2

I'm looking for advice on the best way to represent this JSON object in C#.

To be clear, I'm not trying to convert from an existing JSON string to C# object - this is from scratch. I can visualize what I'm trying to create as JSON, but not sure how that translates...

[
{
   "EquipmentID": "ASD2F",
   "ConnectionIDs":[
   { "ConnectionID": "HD4GH" },
   { "ConnectionID": "KAFD3" },
   { "ConnectionID": "NA3AF" }
   ]
},
{
   "EquipmentID": "GAE31",
   "ConnectionIDs":[
   { "ConnectionID": "HJA03" },
   { "ConnectionID": "FGVA1" },
   { "ConnectionID": "GHAD8" }
   ]
}
]

That's basically an array of EquipmentID's, each EquipmentID containing an array of ConnectionID's. I've been tinkering with a few different Classes containing arrays, lists, etc... but I can't seem to settle on anything. I know this should be incredibly simple, so thanks in advance for helping out!

+6  A: 

Something like this makes sense:

class Equipment {
    public string Id { get; set; }
    public List<string> ConnectionIds { get; set; }
}

Then you'd have a variable of type List<Equipment> to hold the equipments.

By the way, it's not clear to me whether ConnectionIds themselves are equipments or not. If they are, you'd basically have:

class Equipment {
    public string Id { get; set; }
    public List<Equipment> Connections { get; set; }
}
Mehrdad Afshari
ConnectionIds are not equipment, but rather properties of the equipment. This is a rough example of how our datacenter handles equipments in our asset database. Each "equipment" (think server) has multiple connections (i.e., power, network, kvm, etc).Anyway, I think this should work out just right... I think where I was getting confused was in how to consume the class. It didn't occur to me to use the class as a list type.I'll give it a shot tomorrow when I get to the office. Thanks!
SeanW
This worked exactly as I needed, thanks!
SeanW
A: 
class Equipment {
    string id;
    string[] connectionIds;
}
erikkallen