0
我有一個element
類,它包含一個options
財產,期權類又可以改變其屬性,例如可能有這2反序列化動態屬性在ASP.net Web的API
Element1 = {
"Id": "1",
"Options": {
"Printable": "true",
"StackOverflow": "great"
}
}
Element2 = {
"Id": "2",
"Options": {
"Question": "awsome",
"PropertyDiferent": "empty"
}
}
在網頁API我有一個這樣的方法:
Public object Post ([FromBody] Element element)
{
SaveToMongo (element);
}
元素類:
Public class Element
{
Public dynamic options {get; set; }
Public string id {get; set; }
}
當我從Mongo拿起元素時,我沒有任何問題。但是,當我使用Api web的post方法發送它時,它並不反序列化它自己,在expando對象中,因爲它是從Mongo開始的。我怎麼能在兩端得到類似的行爲?
編輯:我試圖將選項從動態更改爲newtonsoft JObject,但它沒有奏效。現在選項也得到了保存,但是他們生成了我不想要的父親。
"options" : {
"_t" : "Newtonsoft.Json.Linq.JObject, Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed",
"_v" : {
"padding" : {
"_t" : "JArray",
"_v" : [
{
"_t" : "JValue",
"_v" : [
]
},
{
"_t" : "JValue",
"_v" : [
]
},
{
"_t" : "JValue",
"_v" : [
]
},
{
"_t" : "JValue",
"_v" : [
]
}
]
},
"image" : {
"_t" : "JValue",
"_v" : [
]
},
"alt" : {
"_t" : "JValue",
"_v" : [
]
},
"url" : {
"_t" : "JValue",
"_v" : [
]
},
"width" : {
"_t" : "JValue",
"_v" : [
]
},
"backgroundColor" : {
"_t" : "JValue",
"_v" : [
]
},
"text" : {
"_t" : "JValue",
"_v" : [
]
}
}
},
有更多的屬性,因爲我直接從mongo中提取示例。
你可以使用Newtonsoft.Json的JObject而不是動態的嗎? –
@PedroDrewanz我會嘗試一下。因爲這幾天我的一些問題都是失敗的。 –