C# 准时制。序列化为json的最佳方法
我需要为库创建一个自定义json。我应该使用额外的C#逻辑还是以某种方式扩展JsonSerializer。Json应该是这样-->C# 准时制。序列化为json的最佳方法,c#,json,serialization,thejit,C#,Json,Serialization,Thejit,我需要为库创建一个自定义json。我应该使用额外的C#逻辑还是以某种方式扩展JsonSerializer。Json应该是这样--> }你想过Json.net吗 至少您将拥有一个良好的定制室+一个更好的序列化程序 公共无效测试() { 节点根=新节点(); 节点子节点=新节点(); 数据数据=新数据(){Area=276,Color=“#8E7032”,PlayCount=“276”,Image=”http://userserve-ak.last.fm/serve/300x300/1140321
}你想过Json.net吗 至少您将拥有一个良好的定制室+一个更好的序列化程序
公共无效测试()
{
节点根=新节点();
节点子节点=新节点();
数据数据=新数据(){Area=276,Color=“#8E7032”,PlayCount=“276”,Image=”http://userserve-ak.last.fm/serve/300x300/11403219.jpg" };
节点孙子=新节点(){Id=“相册第十三步”,Name=“第十三步”,Data=Data};
root.Children.Add(child);
child.Children.Add(孙子);
var json=JsonConvert.SerializeObject(
根,
新的JsonSerializerSettings(){
NullValueHandling=NullValueHandling.Ignore,
格式化=Newtonsoft.Json.Formatting.Indented
});
}
公共类节点
{
[JsonProperty(“儿童”)]
public List Children=new List();
[JsonProperty(“数据”)]
公共数据;
[JsonProperty(“id”)]
公共字符串Id;
[JsonProperty(“名称”)]
公共字符串名称;
}
公共类数据
{
[JsonProperty(“播放计数”)]
公共字符串播放计数;
[JsonProperty(“$color”)]
公共字符串颜色;
[JsonProperty(“图像”)]
公共字符串图像;
[JsonProperty(“$area”)]
公共区域;
}
-使用json的最佳工具是最快的
对于基准测试:您尝试过这个吗?是的,但我需要更多自定义JSON为什么它是“最好的”?你能提供一些背景吗?还是对所有用例来说都是最好的?iwein,你在哪里找到了我自己的网站?
var json = {
"children": [
{
"children": [
{
"children": [],
"data": {
"playcount": "276",
"$color": "#8E7032",
"image": "http://userserve-ak.last.fm/serve/300x300/11403219.jpg",
"$area": 276
},
"id": "album-Thirteenth Step",
"name": "Thirteenth Step"
}
}]
public void Test()
{
Node root = new Node();
Node child = new Node();
Data data = new Data() { Area = 276, Color = "#8E7032", PlayCount = "276", Image = "http://userserve-ak.last.fm/serve/300x300/11403219.jpg" };
Node grandChild = new Node() { Id = "album-Thirteenth Step", Name = "Thirteenth Step", Data = data };
root.Children.Add(child);
child.Children.Add(grandChild);
var json = JsonConvert.SerializeObject(
root,
new JsonSerializerSettings() {
NullValueHandling= NullValueHandling.Ignore,
Formatting= Newtonsoft.Json.Formatting.Indented
});
}
public class Node
{
[JsonProperty("children")]
public List<Node> Children = new List<Node>();
[JsonProperty("data")]
public Data Data;
[JsonProperty("id")]
public string Id;
[JsonProperty("name")]
public string Name;
}
public class Data
{
[JsonProperty("playcount")]
public string PlayCount;
[JsonProperty("$color")]
public string Color;
[JsonProperty("image")]
public string Image;
[JsonProperty("$area")]
public int Area;
}