我有一个DataMember
我需要用api json字符串填充...
[DataContract]
public class Values
{
[DataMember]
public object value { get; set; }
}
API json字符串:
[
{
"type": "text",
"values": [
{
"value": "Text that is in textfield"
}
]
},
{
"type": "category",
"values": [
{
"value": {
"text": "Category title",
"color": "#000000"
}
}
]
}
]
我将此字符串映射到强类型对象Field
,如下所示:
private List<Field> PrepFieldObject(string response)
{
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(response)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Field>));
return (List<Field>)serializer.ReadObject(stream);
}
}
但是当它到达映射Values.value
时,它会引起一个异乎寻常的契合......我试着像这样解决它:
[DataContract]
public class Values
{
[DataMember]
public object value
{
get {
return xamlValue;
}
set
{
xamlValue = new Value();
if( value is string )
{
xamlValue.text = (string)value; // This works fine
}
else
{
Value theValue = value as Value;
try
{
xamlValue.text = theValue.text; // Can't get hold of .text even though it does exist in the json.
xamlValue.color = theValue.color;
}
catch (Exception e)
{
}
}
}
}
public Value xamlValue { get; set; }
}
[DataContract]
public class Value
{
[DataMember]
public string text { get; set; }
[DataMember]
public string color { get; set; }
}
但它不允许我访问对象的属性(我猜因为它们从未被DataContract映射)
我尝试过添加
[KnownType(typeof(Value))]
[KnownType(typeof(string))]
但这也无济于事:'(
答案 0 :(得分:0)
您必须将其强制转换为text
属性所属的任何类型。
((SomeType)value).text
答案 1 :(得分:0)
您必须使用类型转换。示例字符串:
string abc = (string)value.text;
或(最好)
ComplexType comp = value as ComplexType;
<强>更新强>:
当您尝试序列化JSON对象并将其转换为强类型对象(我的ComplexType是其中的一个示例)时,对于属性具有与JSON中相同的名称非常重要。序列化后,您应该能够访问属性值。
希望这有帮助!!!
答案 2 :(得分:0)
您可以使用运算符is
,但必须对value
可以假设的每个值使用它:
if (value is String)
{
//do something
}
else if (value is ...)
...
您可以详细了解is
here。
答案 3 :(得分:0)
您可以使用动态类型:
DataContract]
public class Values
{
[DataMember]
public dynamic value { get; set; }
}
答案 4 :(得分:0)
如果您想要字符串或对象的已定义字符串表示形式,请使用:
String valueAsString = value.ToString()
假设其他类型(除了字符串)重写ToString()方法以提供对象的有意义的字符串表示。
否则你需要使用:
KnownValueType kvt = value as KnownValueType
if(kvt!=null)
{
//access known type properties here
}
答案 5 :(得分:0)
我认为您可以使用反射来访问对象的属性值。 尝试将您的setter的其他部分更改为
else
{
Value theValue = value as Value;
try
{
// pass the object and the property name you're trying to get hold of
xamlValue.text = PropertyHasValue(theValue, "text"); // Can't get hold of .text even though it does exist in the json.
xamlValue.color = PropertyHasValue(theValue, "color");
}
catch (Exception e)
{
}
}
反思方法
// using reflection to get the object's property value
public static String PropertyHasValue(object obj, string propertyName)
{
try
{
if (obj != null)
{
PropertyInfo prop = obj.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
if (prop != null)
{
string sVal = String.Empty;
object val = prop.GetValue(obj, null);
if (prop.PropertyType != typeof(System.DateTime?))
sVal = Convert.ToString(val);
else // format the date to contain only the date portion of it
sVal = Convert.ToDateTime(val).Date.ToString("d"); ;
if (sVal != null)
{
return sVal;
}
}
}
return null;
}
catch
{
return null;
}
}