Reputation: 488
I intend to use DataContractJsonSerializer to convert the json i'm receiving to an object, but the keys in the root can have any name, something similar to this:
{
"Jhon": {...},
"Lucy": {...},
"Robert": {...}
...
}
When the keys are fixed i can use [DataMember(Name = "keyname")]
but in this case I don't know what to do. Any ideas?
Upvotes: 0
Views: 359
Reputation: 14477
Try this:
var serializer = new DataContractJsonSerializer(typeof(RootObject), new DataContractJsonSerializerSettings()
{
UseSimpleDictionaryFormat = true
});
var json = @"{
""Jhon"": { ""Name"": ""John""},
""Lucy"": {},
""Robert"": {}
}";
var bytes = Encoding.UTF8.GetBytes(json);
using (var stream = new MemoryStream(bytes))
{
var results = serializer.ReadObject(stream);
}
// Define other methods and classes here
public class RootObject : Dictionary<string, User>
{
}
public class User
{
public string Name { get; set; }
}
Upvotes: 2