使用JSON.NET序列化为数组

时间:2015-02-04 17:43:28

标签: c# json.net

如何让Json.NET序列化程序将实例序列化为对象数组?

基本上我需要这样的东西:

[
    {
      "name":"Page1.html",
      "size":1,
      "outlinks":[
        "Page2.html",
        "Page3.html",
        "Page4.html"
      ]
    },
    {
      "name":"Page2.html",
      "size":2,
      "outlinks":[
        "Page3.html"
      ]
    },
    {
      "name":"Page3.html",
      "size":3,
      "outlinks":[
        "Page1.html",
        "Page2.html",
        "Page3.html",
        "Page4.html"
      ]
    },
    {
       "name":"Page4.html",
       "size":4,
        "outlinks":[]
    }
]

使用:

    Dictionary<string, string[]> UrlsCollection = new Dictionary<string, string[]>();
    List<string> OutLinks = new  List<string>();

    OutLinks.Add("Page2.html");
    OutLinks.Add("Page3.html");
    OutLinks.Add("Page4.html");
    UrlsCollection.Add("Page1.html", OutLinks.ToArray());
    OutLinks.Clear();

    OutLinks.Add("Page3.html");
    UrlsCollection.Add("Page2.html", OutLinks.ToArray());
    OutLinks.Clear();

    OutLinks.Add("Page1.html");
    OutLinks.Add("Page2.html");
    OutLinks.Add("Page3.html");
    OutLinks.Add("Page4.html");
    UrlsCollection.Add("Page3.html", OutLinks.ToArray());
    OutLinks.Clear();

    UrlsCollection.Add("Page4.html", OutLinks.ToArray());
    OutLinks.Clear();

    string jsonUrlsCollection = JsonConvert.SerializeObject(UrlsCollection.ToList(), Formatting.Indented);

我明白了:

[
  {
    "Key": "Page1.html",
    "Value": [
      "Page2.html",
      "Page3.html",
      "Page4.html"
    ]
  },
  {
    "Key": "Page2.html",
    "Value": [
      "Page3.html"
    ]
  },
  {
    "Key": "Page3.html",
    "Value": [
      "Page1.html",
      "Page2.html",
      "Page3.html",
      "Page4.html"
    ]
  },
  {
    "Key": "Page4.html",
    "Value": []
  }
]

必须有一种方法可以获得一个简单的JSON对象吗?

修改解决方案以反映Deblaton Jean-Philippe在评论中的建议。

public class UrlDef
{
    public UrlDef() { outlinks = new List<string>();  }
    public string name { get; set; }
    public int size { get; set; }
    public List<string> outlinks { get; set; }
}

    List<UrlDef> UrlsCollection = new List<UrlDef>();

    UrlDef urldef;

    urldef = new UrlDef();
    urldef.name = "Page1.html";
    urldef.size = 1;
    urldef.outlinks.Add("Page2.html");
    urldef.outlinks.Add("Page3.html");
    urldef.outlinks.Add("Page4.html");
    UrlsCollection.Add(urldef);

    urldef = new UrlDef();
    urldef.name = "Page2.html";
    urldef.size = 2;
    urldef.outlinks.Add("Page3.html");
    UrlsCollection.Add(urldef);

    urldef = new UrlDef();
    urldef.name = "Page3.html";
    urldef.size = 3;
    urldef.outlinks.Add("Page1.html");
    urldef.outlinks.Add("Page2.html");
    urldef.outlinks.Add("Page3.html");
    urldef.outlinks.Add("Page4.html");
    UrlsCollection.Add(urldef);

    urldef = new UrlDef();
    urldef.name = "Page4.html";
    urldef.size = 4;
    UrlsCollection.Add(urldef);

    string jsonUrlsCollection = JsonConvert.SerializeObject(UrlsCollection, Formatting.Indented);

1 个答案:

答案 0 :(得分:4)

您需要序列化此对象的列表

public class Url
{
    public string name { get; set; }
    public int size { get; set; }
    public List<string> outlinks { get; set; }
}

对于这个答案,我使用了一个我经常使用的网站,当我知道我对JS的期望时,但我不确定我需要在CS中提供什么:http://json2csharp.com/