As the object contains properties that cannot be legally named in C# you need to either wrap them as string indexes or use an attribute the specify the property name.
Firstly, you need to declare the objects to use
public class Foo
{
public Dictionary<string, List<Bar>> addresses { get; set; }
}
public class Bar
{
public int version { get; set; }
public string addr { get; set; }
[DataMember(Name = "OS-EXT-IPS:type")]
public string OstType { get; set; }
}
I have opted to use a Dictionary<string, List<Bar>> combination for the list of addresses. The key will be 10.0.0.1/24 in your example. The property OST-EXT-IPS:type cannot be legally translated, so this uses the attribute option.
The class can then be deseralised as
public static string JsonExtract()
{
Foo obj = new Foo();
obj.addresses = new Dictionary<string, List<Bar>>() { { "10.0.0.1/24", new List<Bar>() { new Bar() { version = 4, addr = "10.0.0.36", OstType = "fixed" } } }};
JavaScriptSerializer js = new JavaScriptSerializer();
string s = js.Serialize(obj);
return s;
}
public static Foo JsonParse()
{
string file = @"json.txt"; // source
using (StreamReader rdr = new StreamReader(file))
{
string json = rdr.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
Foo obj = js.Deserialize<Foo>(json);
return obj;
}
}
This has been done using the JavaScriptSeralizer in the System.Web.Script.Serialization namespace, but a similar approach can be done via Netonsoft Json library or other libraries in use.
The key is to ensure that properties are mapped to a json attribute as either sting key of an object or via a serialisation attribute.
addressesbe an array? Are you sure that always only 1 ip address range is coming in your json?