7

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

}

4

4 回答 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;
}
于 2012-06-15T13:22:17.603 回答
1

你有关于 Json.net 的想法吗?

http://json.codeplex.com/

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

于 2012-06-15T12:33:23.000 回答
1

json - 使用 json 的最佳工具

于 2012-10-22T18:13:55.843 回答
0

ServiceStack.Text 是最快的。

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

于 2013-10-02T12:37:07.433 回答