如何在一行中写一个json属性的值?

How to write the value of one json property in one line?

有必要在 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中对应class对象的函数:

JToken ToJson()
        {
            using (var writer = new JTokenWriter()) {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(writer, this);
                return writer.Token;
            }
        }

您的第二个案例似乎包含坐标 属性 作为序列化字符串。

为什么不应该使用 var string = JsonConvert.SerializeObject(YourObject) ?

但是你应该先安装https://www.nuget.org/packages/Newtonsoft.Json/

对于需要双引号的属性可以使用字符串类型,如果不需要则使用数组或数字