1
類似於question,我有一個包含BsonDocument的幾個不同屬性類的類。將非結構化的BsonDocument作爲ApiController中的類屬性返回
public class Report
{
[BsonId, JsonIgnore]
public ObjectId _id { get; set; }
public string name { get; set; }
[JsonIgnore]
public BsonDocument layout { get; set; }
[BsonIgnore, JsonProperty(PropertyName = "layout")]
public string layout2Json
{
get { return layout.ToJson(); }
}
}
原因在那裏有BsonDocument,是佈局屬性是非結構化的,我不能有任何的強類型的子類。現在,當ApiController返回這個類,我得到的是這樣的:
{
name: "...",
layout: "{type: "...", sth: "..."}"
}
但我需要的是佈局屬性爲對象,而不是字符串。
在JSON.NET中是否有一種方法將json-string(已經是有效的json)插入爲對象而不是字符串?
下面的作品,但似乎很浪費:
[BsonIgnore, JsonProperty(PropertyName = "layout")]
public JObject layout2Json
{
get { return JObject.Parse(layout.ToJson()); }
}