如何创建这个Json?

时间:2013-11-18 23:52:02

标签: c# json

说我有这个硬编码的代码,

$("#test").gmap3({
  map:{
    options:{
      center:{lat:24.886436490787712,lng:-70.2685546875},
      zoom:3,
      mapTypeId: google.maps.MapTypeId.TERRAIN
    }
  },
  polygon: {
    values: [
      {
        options:{
          strokeColor: "#FF0000",
          strokeOpacity: 0.8,
          strokeWeight: 2,
          fillColor: "#FF0000",
          fillOpacity: 0.35,
          paths:[
            [25.774252, -80.190262],
            [18.466465, -66.118292],
            [32.321384, -64.75737],
            [25.774252, -80.190262]
          ]
        }
      },
      {
        options:{
          strokeColor: "#FF0000",
          strokeOpacity: 0.8,
          strokeWeight: 2,
          fillColor: "#FF0000",
          fillOpacity: 0.35,
          paths:[
            [37.33522435930639,-97.7783203125],
            [37.33522435930639,-85.8251953125],
            [29.420460341013133,-86.3525390625],
            [23.120153621695614,-97.0751953125]
          ]
        }
      },
      {
        options:{
          strokeColor: "#FF0000",
          strokeOpacity: 0.8,
          strokeWeight: 2,
          fillColor: "#FF0000",
          fillOpacity: 0.35,
          paths:[
            [21.002471054356725,-52.4267578125],
            [28.34306490482549,-47.1533203125],
            [17.35063837604883,-35.7275390625],
            [11.049038346537106,-49.0869140625],
            [8.276727101164045,-61.2158203125]
          ]
        }
      },
    ],
    onces:{
      click: function(polygon){
        polygon.setOptions({
            fillColor: "#FFAF9F",
            strokeColor: "#FF512F"
        });
      }
    }
  }
});

我想知道如何从C#代码创建“值”部分。

到目前为止,我有这个。

public class Options
{
    public string strokeColor { get; set; }
    public string strokeWeight { get; set; }
    public string fillColor { get; set; }
    public string fillOpacity { get; set; }
    public string strokeOpacity { get; set; }
}

我想这将是一个集合(或数组)但不确定路径。我就是这样。它必须是数组中的某种数组,但它不能具有任何属性名称。

1 个答案:

答案 0 :(得分:6)

这可能是你想要的:

public class Options
{
    public string strokeColor { get; set; }
    public double strokeOpacity { get; set; }
    public int strokeWeight { get; set; }
    public string fillColor { get; set; }
    public double fillOpacity { get; set; }
    public List<List<double>> paths { get; set; }
}

public class RootObject
{
    public Options options { get; set; }
}

顺便说一句:我通过将JSON粘贴到这个小工具中来创建这些类: http://json2csharp.com/

显然,你需要将其反序列化为JSON,但我想你知道如何做到这一点。