且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用JSON.net附加到JSON对象

更新时间:2023-01-17 09:10:39

如果将dataFrmDb更改为Dictionary<string, object>而不是Dictionary<string, string>,则可以像其他值一样将国家/地区"列表存储到其中.然后,Json.Net将根据需要对其进行序列化.

If you change dataFrmDb to be Dictionary<string, object> instead of a Dictionary<string, string>, then you can store the Countries list into it like the other values. Json.Net will then serialize it like you want.

这是一个示例程序,演示:

Here is an example program which demonstrates:

class Program
{
    static void Main(string[] args)
    {
        List<Dictionary<string, object>> dataResults = GetResults();           
        Dictionary<string, List<Dictionary<string, object>>> myList = 
                      new Dictionary<string, List<Dictionary<string, object>>>();
        myList.Add("Documents", dataResults);
        string ans = JsonConvert.SerializeObject(myList, Formatting.Indented);
        System.Console.WriteLine(ans); 
    }

    public static List<Dictionary<string, object>> GetResults()
    {
        List<Dictionary<string, object>> dictList = new List<Dictionary<string, object>>();

        Dictionary<string, object> dataFrmDb = new Dictionary<string, object>();
        dataFrmDb.Add("Title", "An Example Document");
        dataFrmDb.Add("DatePublished", DateTime.Now.ToString());
        dataFrmDb.Add("DocumentURL", "http://www.example.org/documents/1234");
        dataFrmDb.Add("ThumbnailURL", "http://www.example.org/thumbs/1234");
        dataFrmDb.Add("Abstract", "This is an example document.");
        dataFrmDb.Add("Sector", "001");
        dataFrmDb.Add("Country", new List<string> { "USA", "Bulgaria", "France" });
        dataFrmDb.Add("Document Type", "example");

        dictList.Add(dataFrmDb);
        return dictList;
    }
}

输出:

{
  "Documents": [
    {
      "Title": "An Example Document",
      "DatePublished": "4/9/2013 7:25:05 PM",
      "DocumentURL": "http://www.example.org/documents/1234",
      "ThumbnailURL": "http://www.example.org/thumbs/1234",
      "Abstract": "This is an example document.",
      "Sector": "001",
      "Country": [
        "USA",
        "Bulgaria",
        "France"
      ],
      "Document Type": "example"
    }
  ]
}

如Joey Gennari所建议的那样,一种更直接的方法是创建单独的类来保存数据. Json.NET也可以序列化它们.数据类如下所示:

A somewhat more straightforward way to do it is to create separate classes to hold the data, as was suggested by Joey Gennari. Json.NET can serialize those as well. The data classes would look something like this:

class Result
{
    public List<Document> Documents { get; set; }

    public Result()
    {
        Documents = new List<Document>();
    }
}

class Document
{
    public string Title { get; set; }
    public string DatePublished { get; set; }
    public string DocumentURL { get; set; }
    public string ThumbnailURL { get; set; }
    public string Abstract { get; set; }
    public string Sector { get; set; }
    public List<string> Country { get; set; }
    [JsonProperty(PropertyName="Document Type")]
    public string DocumentType { get; set; }

    public Document()
    {
        Country = new List<string();
    }
}

这是用法:

class Program
{
    static void Main(string[] args)
    {
        Document doc = new Document();
        doc.Title = "An Example Document";
        doc.DatePublished = DateTime.Now.ToString();
        doc.DocumentURL = "http://www.example.org/documents/1234";
        doc.ThumbnailURL = "http://www.example.org/thumbs/1234";
        doc.Abstract = "This is an example document.";
        doc.Sector = "001";
        doc.Country.Add("USA");
        doc.Country.Add("Bulgaria");
        doc.Country.Add("France");
        doc.DocumentType = "example";

        Result result = new Result();
        result.Documents.Add(doc);

        string json = JsonConvert.SerializeObject(result, Formatting.Indented);
        System.Console.WriteLine(json);
    }
}

此示例的输出与第一个示例完全相同.

The output for this example is exactly the same as the first.