【问题标题】:Serialize as array with JSON.NET使用 JSON.NET 序列化为数组
【发布时间】:2015-02-04 17:43:27
【问题描述】:

如何让 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 在 cmets 中的建议。

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

【问题讨论】:

  • 不要使用Dictonary
  • 你已经得到的是一个对象数组。这些对象不是您期望的那样。

标签: c# json.net


【解决方案1】:

你需要序列化这个对象的列表

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

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

【讨论】:

  • 感谢让-菲利普。我按照你的建议更新了我的帖子。我不知道这个网站。这很有帮助!
  • 您更新帖子时还有问题吗?
猜你喜欢
  • 1970-01-01
  • 2016-08-20
  • 1970-01-01
  • 1970-01-01
  • 2017-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多