У меня есть DataMember
, который мне нужно заполнить строкой API JSON...
[DataContract]
public class Values
{
[DataMember]
public object value { get; set; }
}
JSON-строка API:
[
{
"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))]
но и это не помогает :'(
value
. - person Servy   schedule 12.06.2014