Note that there are some explanatory texts on larger screens.

plurals
  1. POAppending to JSON object using JSON.net
    primarykey
    data
    text
    <p>I need to serialize a JSON object that looks like this: </p> <pre><code>{ "Documents": [ { "Title": "", "DatePublished": "", "DocumentURL": "", "ThumbnailURL": "", "Abstract": "", "Sector": "", "Country": [ "", "", "" ], "Document Type": "" } ] } </code></pre> <p>What I'm doing is taking the data from SQL server and storing the results into an object like this: </p> <pre><code>public List&lt;Dictionary&lt;string, string&gt;&gt; GetResults() { int index = 0; while (this.myReader.Read()) { this.dataFrmDb = new Dictionary&lt;string, string&gt;(); for (int i = 0; i &lt; myReader.FieldCount; i++) { if (myReader.GetName(i) == "Country") { string[] delimiter = { " _qfvcq_ " }; string text = myReader[myReader.GetName(i)].ToString(); string[] results = text.Split(delimiter, StringSplitOptions.None); //This list stores the values for "Country". List&lt;string&gt; countries = new List&lt;string&gt;(); for (int j = 0; j &lt; results.Count(); j++) { countries.Add(results[j].ToString()); } } else { this.dataFrmDb.Add(myReader.GetName(i), myReader[myReader.GetName(i)].ToString()); } } this.dictList.Add(this.dataFrmDb); } return this.dictList; } </code></pre> <p>I then take this data and serialize like this: </p> <pre><code>Database connect = new Database( System.Configuration.ConfigurationManager.AppSettings["DatabaseConnectionString"], System.Configuration.ConfigurationManager.AppSettings["StoredProcedure"]); List&lt;Dictionary&lt;string, string&gt;&gt; dataResults = connect.GetResults(); Dictionary&lt;string, List&lt;Dictionary&lt;string, string&gt;&gt;&gt; myList = new Dictionary&lt;string, List&lt;Dictionary&lt;string, string&gt;&gt;&gt;(); myList.Add("Documents", dataResults); string ans = JsonConvert.SerializeObject(myList, Formatting.Indented); System.Console.WriteLine(ans); </code></pre> <p>I get the proper output but if you would look in the original JSON format, "Country" needs to have multiple values. I don't know how to implement that into this JSON object. How do I add a list with the "Country" values to the JSON object using JSON.net? Is there another way to go about this?</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.
    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