The question is similar to Deserializing JSON with unknown fields but I would like to use the built in DataContractJsonSerializer instead.
So I have JSON data like that:
{
"known1": "foo",
"known2": "bar",
"more":{ "unknown12345": { "text": "foo", "label": "bar"},
"unknown67890": { "text": "foo","label":"bar"}
}
}
I thought I can do the datacontract like that:
[DataMember(Name = "known1")]
public string K1 { get; set; }
[DataMember(Name = "known2")]
public string K2 { get; set; }
[DataMember(Name = "more")]
public Dictionary<string,TwoStringMembersClass> More { get; set; }
And the TwoStringMembersClass is just this:
[DataContract(Name = "TwoStringMembersClass ")]
public class TwoStringMembersClass
{
[DataMember(Name = "text")]
public string Text { get; set; }
[DataMember(Name = "label")]
public string Label { get; set; }
}
But what seems to work in JSON.Net doesn't seem to work that easy with the native JSON parser. In ReadObject() I get an ArgumentException, probably because of the Dictionary.
Any idea what's the best solution how to make this work ?
Thanks in advance.