Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you change <code>dataFrmDb</code> to be <code>Dictionary&lt;string, object&gt;</code> instead of a <code>Dictionary&lt;string, string&gt;</code>, then you can store the Countries list into it like the other values. Json.Net will then serialize it like you want.</p> <p>Here is an example program which demonstrates:</p> <pre><code>class Program { static void Main(string[] args) { List&lt;Dictionary&lt;string, object&gt;&gt; dataResults = GetResults(); Dictionary&lt;string, List&lt;Dictionary&lt;string, object&gt;&gt;&gt; myList = new Dictionary&lt;string, List&lt;Dictionary&lt;string, object&gt;&gt;&gt;(); myList.Add("Documents", dataResults); string ans = JsonConvert.SerializeObject(myList, Formatting.Indented); System.Console.WriteLine(ans); } public static List&lt;Dictionary&lt;string, object&gt;&gt; GetResults() { List&lt;Dictionary&lt;string, object&gt;&gt; dictList = new List&lt;Dictionary&lt;string, object&gt;&gt;(); Dictionary&lt;string, object&gt; dataFrmDb = new Dictionary&lt;string, object&gt;(); 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&lt;string&gt; { "USA", "Bulgaria", "France" }); dataFrmDb.Add("Document Type", "example"); dictList.Add(dataFrmDb); return dictList; } } </code></pre> <p>Output:</p> <pre><code>{ "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" } ] } </code></pre> <p>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:</p> <pre><code>class Result { public List&lt;Document&gt; Documents { get; set; } public Result() { Documents = new List&lt;Document&gt;(); } } 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&lt;string&gt; Country { get; set; } [JsonProperty(PropertyName="Document Type")] public string DocumentType { get; set; } public Document() { Country = new List&lt;string(); } } </code></pre> <p>And here is the usage:</p> <pre><code>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); } } </code></pre> <p>The output for this example is exactly the same as the first.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload