I have the following json (provided by an API):
[
{
"type": "text",
"values": [
{
"value": "Text that is in textfield"
}
]
},
{
"type": "category",
"values": [
{
"value": {
"text": "Category title",
"color": "#000000"
}
}
]
}
]
How do you present such an object as strong type (which C# requires of you) when values.value is of dynamic?
Here is more information on my issue: Trying to map two different types to one value
but I wanted to keep this new question lean.
Sorry to bump, but I still can't get this to work! Any examples of how to serialize this?
I currently have this code to serialize:
[DataContract]
public class MyField
{
[DataMember]
public List<MyValue> values { get; set; }
}
[DataContract]
public class MyValue
{
[DataMember]
public object value { get; set; } // This will crash because it needs to be both a string and an object with text,color properties??
}
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(response)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<MyField>));
return (List<MyField>)serializer.ReadObject(stream);
}
but it crashes on the values.value bit because it cannot cast to strongly typed... as it is varying.
////edit////
Sureeeely someone has done this before!!!