以下几行中必须在不带连字符的情况下在json中写入“ Coordinates”属性的值,而不使用ToString()(无需将值转换为字符串)。所需的结果如下所示。
{
"Id": null,
"Style": "1234",
"Geometry": {
"Type": "Polygon",
"Coordinates": [[[47541.470259278358,6846.8710054924586],[47540.359922950891,6845.4552435801925],[47541.470259278358,6846.8710054924586]]],
"Properties": [
{
"PointType": "Straight"
},
{
"PointType": "Straight"
},
{
"PointType": "Straight"
}
]
}
}
但不是:
{
"Id": null,
"Style": "1234",
"Geometry": {
"Type": "Polygon",
"Coordinates": "[[[47541.470259278358,6846.8710054924586],[47540.359922950891,6845.4552435801925],[47541.470259278358,6846.8710054924586]]]",
"Properties": [
{
"PointType": "Straight"
},
{
"PointType": "Straight"
},
{
"PointType": "Straight"
}
]
}
}
一个用于序列化json中相应类对象的函数:
JToken ToJson()
{
using (var writer = new JTokenWriter()) {
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, this);
return writer.Token;
}
}
答案 0 :(得分:0)
似乎您的第二种情况包含Coordinates属性作为序列化字符串。
为什么不应该使用
var string = JsonConvert.SerializeObject(YourObject)
吗?
但是您应该先安装https://www.nuget.org/packages/Newtonsoft.Json/
您可以在需要双引号的属性中使用字符串类型,如果不需要,可以使用数组或数字