Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dictionary object into Json string

I have a dictionary object which is declared as shown below.

Dictionary<string, Dictionary<int, List<DataRow>>> lineList = new Dictionary<string, Dictionary<int, List<DataRow>>>();

I want to convert this object into Json string but when I used following code, It only considered first object from dictionary list and then added rest of the data in it regardless of for which key it is connected.

var linechartString = JsonConvert.SerializeObject(lineList);

I would like to know if there is a different function available which can convert my dictionary object into JSON string.

like image 457
romie99 Avatar asked Dec 12 '16 18:12

romie99


2 Answers

This sample serializes a dictionary to JSON

Dictionary<string, int> points = new Dictionary<string, int>
{
    { "Ali", 1111},
    { "Hasan", 2222},
    { "HoseynJan", 3333 }
};

string json = JsonConvert.SerializeObject(points, Formatting.Indented);

Console.WriteLine(json);
// {
//   "Ali": 1111,
//   "Hasan": 2222,
//   "HoseynJan": 3333
// }
like image 123
Morteza Sefidi Avatar answered Oct 29 '22 02:10

Morteza Sefidi


Look at this link

http://www.newtonsoft.com/json/help/html/serializedictionary.htm

The second way

Dictionary>**>

Dictionary> you can try to Serialize firsly For each Main Dictionary element. After this, Serialize Main Dictionary ;) I hope this will hep

like image 42
Eyup Can ARSLAN Avatar answered Oct 29 '22 02:10

Eyup Can ARSLAN