Update
Given your updated classes and updated JSON, you would do:
public class RootObject
{
public Root root { get; set; }
}
public class Root
{
public int id {set;get;}
public string name {set;get;}
public Dictionary<string, Container> containers { get; set; }
}
public class Container
{
public int id {set; get;}
public string name {set;get;}
public int value {set;get;}
}
And use it like:
var root = JsonConvert.DeserializeObject<RootObject>(json);
Note that the "root" property in the JSON requires an extra level of indirection, which us provided by the RootObject class. You might want to rename your Root to something more descriptive like RootContainer.
Update 2
The JSON in the question was modified again to eliminate the "root" property, so RootObject is unnecessary and you just need to do:
var root = JsonConvert.DeserializeObject<Root>(json);
Original Answer
Assuming your nested containers are also of type Container, you can just deserialize them as a Dictionary<string, Container> property:
public class Container
{
public int id { set; get; }
public string name { set; get; }
public int? value { set; get; } // Null when the property was not present in the JSON.
public Dictionary<string, Container> containers { get; set; }
}
You would use it like:
public static void Test()
{
string json = @"
{
""id"" : 3223,
""name"" : ""position 3223"",
""containers"" : {
""container_demo"" : {
""id"" : 12,
""name"" : ""demo"",
""value"" : 34
},
""different_name_1"" : {
""id"" : 33,
""name"" : ""demo 3"",
""value"" : 1
},
""another_contaier"" : {
""id"" : 1,
""name"" : ""another demo"",
""value"" : 34
}
}
}";
var container = JsonConvert.DeserializeObject<Container>(json);
JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
Debug.WriteLine(JsonConvert.SerializeObject(container, Formatting.Indented, settings));
}
This produces the output:
{
"id": 3223,
"name": "position 3223",
"containers": {
"container_demo": {
"id": 12,
"name": "demo",
"value": 34
},
"different_name_1": {
"id": 33,
"name": "demo 3",
"value": 1
},
"another_contaier": {
"id": 1,
"name": "another demo",
"value": 34
}
}
As you can see, all data was deserialized and serialized successfully.
containersproperty is a generic type? Detail your classes, please.