简体   繁体   English

将字典对象转换为Json字符串

[英]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. 我想将此对象转换为Json字符串,但是当我使用下面的代码时,它只考虑字典列表中的第一个对象,然后在其中添加其余数据,而不管它连接的是哪个键。

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. 我想知道是否有不同的函数可以将我的字典对象转换为JSON字符串。

Look at this link 看看这个链接

http://www.newtonsoft.com/json/help/html/serializedictionary.htm 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 在此之后,序列化主要词典;)我希望这将是肝脏

This sample serializes a dictionary to JSON 此示例将字典序列化为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
// }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM