Skip to main content
 首页 » 编程设计

c#之使用 JSON.NET 序列化为数组

2024年02月06日11xiaohuochai

如何让 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 对象吗?

修改解决方案以反射(reflect) 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); 

请您参考如下方法:

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

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

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