【发布时间】:2014-06-18 06:02:39
【问题描述】:
我有以下 json(由 API 提供):
[
{
"type": "text",
"values": [
{
"value": "Text that is in textfield"
}
]
},
{
"type": "category",
"values": [
{
"value": {
"text": "Category title",
"color": "#000000"
}
}
]
}
]
当 values.value 是动态的时,如何将这样的对象呈现为强类型(C# 要求你)?
以下是有关我的问题的更多信息: Trying to map two different types to one value
但我想让这个新问题保持精简。
对不起,我仍然无法让它工作!任何如何序列化的例子?
我目前有这个代码要序列化:
[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);
}
但它在 values.value 位上崩溃,因为它无法转换为强类型...因为它在变化。
////编辑////
肯定有人这样做过!!!
【问题讨论】:
标签: c# json xaml windows-phone-8