【问题标题】:JIT. Best way serialize to json即时通讯。序列化为 json 的最佳方式
【发布时间】:2012-06-18 11:48:27
【问题描述】:

我需要为 the jit 库创建一个自定义 json。我应该使用额外的 C# 逻辑还是以某种方式扩展 JsonSerializer。 Json应该是这样的-->

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"
     }
}] 

}

【问题讨论】:

标签: c# json serialization thejit


【解决方案1】:

你有想过 Json.net 吗?

http://json.codeplex.com/

至少你会有一个很好的定制空间+一个更好的序列化器

【讨论】:

    【解决方案2】:

    使用Json.Net

    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;
    }
    

    【讨论】:

      【解决方案3】:

      json - 使用 json 的最佳工具

      【讨论】:

      • 为什么是“最好的”?你能提供一些上下文吗?还是最适合所有用例?
      • iwein,你在哪里找到我自己的网站?
      【解决方案4】:

      ServiceStack.Text 是最快的。

      对于基准测试:http://www.servicestack.net/benchmarks/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 2010-09-22
        • 1970-01-01
        • 2018-08-20
        • 2015-09-01
        • 2018-10-25
        • 2010-10-25
        相关资源
        最近更新 更多