【发布时间】:2013-09-18 14:35:33
【问题描述】:
我有一个动态对象被用作ApiController 的参数。即:
public class Shape
{
public dynamic Coordinates { get; set; }
public string Id { get; set; }
public string Type { get; set; }
}
任何形状的坐标都是不同的,圆有圆心和半径,直线有x1、y1、x2、y2等。
我正在尝试将此对象存储在 Mongo 中。
我希望的是:
{
"Shapes": [
{
"Coordinates": {
"x1": 1,
"y1": 2,
"x2": 3,
"y2": 4
}
},
"Type": "line"
},
{
"Coordinates": "{ "x" : 10, "y" : 20, "r" : 30,},
"Type": "circle"
}
],
}
当我使用BsonExtensionMethods.ToJson(coordinates) 我得到
{
"Shapes": [
{
"Coordinates": "{ \"x1\" : [], \"y1\" : [], \"x2\" : [], \"y2\" : [] }",
"Type": "line"
}
],
}
当我使用(JObject) coordinates 时,我得到:
{
"Shapes": [
{
"Coordinates": {
"_t": "Newtonsoft.Json.Linq.JObject, Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed",
"_v": {
"x1": [
],
"y1": [
],
"x2": [
],
"y2": [
]
}
},
"Type": "line"
}
],
}
我宁愿不必将其存储为字符串。如何让 .NET 相信我要存储动态对象的值?
【问题讨论】:
-
您介意粘贴创建和填充 Shape 类实例的代码吗?
标签: .net mongodb json.net .net-4.5 mongodb-.net-driver