JIT. 最佳的序列化为JSON的方式

7

我需要为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"
     }
}] 

}


你尝试过这个吗 - http://msdn.microsoft.com/zh-cn/library/system.web.script.serialization.javascriptserializer.aspx - Angshuman Agarwal
1
是的,但我需要更多定制的JSON。 - Alexandr
4个回答

4
使用 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;
}

1

你有没有考虑过使用Json.net?

http://json.codeplex.com/

至少您将拥有更好的自定义空间和更好的序列化器


1

json - 处理 JSON 数据的最佳工具


1
为什么它是“最好的”?你能提供一些上下文吗?或者它适用于所有用例吗? - Emil Vikström
iwein,你在哪里找到了我的网站? - user787230

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接