将C#对象序列化为json
创建可序列化为的C#类的最佳方法是什么:将C#对象序列化为json,c#,json,serialization,json.net,C#,Json,Serialization,Json.net,创建可序列化为的C#类的最佳方法是什么: marks: [ { c: [57.162499, 65.54718], // latitude, longitude - this type of coordinates works only for World Map tooltip: 'Tyumen - Click to go to http://yahoo.com', // text for tooltip
marks: [
{ c: [57.162499, 65.54718], // latitude, longitude - this type of coordinates works only for World Map
tooltip: 'Tyumen - Click to go to http://yahoo.com', // text for tooltip
attrs: {
href: 'http://yahoo.com', // link
src: '/markers/pin1_red.png' // image for marker
}
},
{ xy: [50, 120], // x-y coodinates - works for all maps, including World Map
tooltip: 'This is London! Click to go to http://london.com',
attrs: {
href: 'http://yahoo.com', // link
src: '/markers/pin1_yellow.png' // image for marker
}
}
]
在上面的代码中,我们可以指定“c”或“xy”,但不能同时指定两者。
我正在使用Newtonsoft.Json。
我唯一想要的是一个可以序列化为上述代码的C类。该类如下所示:
[Serializable]
public class Marks
{
public List<latlon> marks = new List<latlon>();
}
public class latlon
{
public double[] c;
public int[] xy;
public string tooltip;
public attributes attrs;
public latlon (double lat, double lon)
{
c = new double[] { lat, lon };
}
public latlon (int x, int y)
{
xy = new int[] { x, y };
}
}
public class attributes
{
public string href;
public string src;
}
string json;
Marks obj = new Marks();
latlon mark = new latlon(57.162, 65.547)
{
tooltip = "Tyumen - Click to go to http://yahoo.com",
attrs = new attributes()
{
href = "http://yahoo.com",
src = "/markers/pin1_red.png"
}
};
obj.marks.Add(mark);
mark = new latlon(50, 120)
{
tooltip = "This is London! Click to go to http://london.com",
attrs = new attributes()
{
href = "http://yahoo.com",
src = "/markers/pin1_yellow.png"
}
};
obj.marks.Add(mark);
//serialize to JSON, ignoring null elements
json = JsonConvert.SerializeObject(obj, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
该类如下所示:
[Serializable]
public class Marks
{
public List<latlon> marks = new List<latlon>();
}
public class latlon
{
public double[] c;
public int[] xy;
public string tooltip;
public attributes attrs;
public latlon (double lat, double lon)
{
c = new double[] { lat, lon };
}
public latlon (int x, int y)
{
xy = new int[] { x, y };
}
}
public class attributes
{
public string href;
public string src;
}
string json;
Marks obj = new Marks();
latlon mark = new latlon(57.162, 65.547)
{
tooltip = "Tyumen - Click to go to http://yahoo.com",
attrs = new attributes()
{
href = "http://yahoo.com",
src = "/markers/pin1_red.png"
}
};
obj.marks.Add(mark);
mark = new latlon(50, 120)
{
tooltip = "This is London! Click to go to http://london.com",
attrs = new attributes()
{
href = "http://yahoo.com",
src = "/markers/pin1_yellow.png"
}
};
obj.marks.Add(mark);
//serialize to JSON, ignoring null elements
json = JsonConvert.SerializeObject(obj, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
你想要一个有两个属性的类还是两个类?@WouterdeKort:实际上是一个类4属性{c,xy,tooltip,attrs}。你想要一个有两个属性的类还是两个类?@WouterdeKort:实际上是一个类4属性{c,xy,tooltip,attrs}。