如何添加值到词典与里面的字典列表 [英] How to add values to Dictionary with a list of dictionaries inside

查看:158
本文介绍了如何添加值到词典与里面的字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在尝试添加这个复杂对象的值。我试图使用JSON.Net将此对象转换成JSON。

 字典< string,List< Dictionary< string,int>>> MyList 
=新词典< string,List< Dictionary< string,int>>>();

最终结果应如下所示: {
文件:[
{
标题:
DatePublished:
DocumentURL:
ThumbnailURL:
摘要:
部门:,
国家:[

],
文档类型:
}

我已经尝试过但没有工作:

  MyList [字典] [0] .Add(test,4); 


解决方案

如果你有一个 ; string,int> dictA 您可以通过

 字典< string,int> dictA = new Dictionary< string,int>(); 
dictA.Add(SomeString,99);

然后将此字典添加到您的列表<字典< string,int> > 使用以下

 列表<字典< string,int>> dictList = new List< Dictionary< string,int>>(); 
dictList.Add(dictA);

现在您可以将其添加到您的 Dictionary< string,List< Dictionary& string,int>>> myList 对象通过

 字典< string,List< Dictionary< string,int>>> myList = 
new Dictionary< string,List< Dictionary< string,int>>>();
myList.Add(SomeString,dictList);

我希望这有帮助。


Hello I'm trying to add values to this complex object. I'm trying to turn this object into JSON using JSON.Net.

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

The end result should look something like this: { "Documents": [ { "Title": , "DatePublished": , "DocumentURL": , "ThumbnailURL": , "Abstract": , "Sector": "", "Country": [ "" ], "Document Type": "" } I have tried this but didn't work:

MyList["Dictionary"][0].Add("test", 4);

解决方案

If you have a Dictionary<string, int> dictA you can add items to this via

Dictionary<string, int> dictA = new Dictionary<string, int>();
dictA.Add("SomeString", 99);

Then to add this dictionary to your List<Dictionary<string, int>> use the following

List<Dictionary<string, int>> dictList = new List<Dictionary<string, int>>();
dictList.Add(dictA);

Now you can add this to your Dictionary<string, List<Dictionary<string, int>>> myList object via

Dictionary<string, List<Dictionary<string, int>>> myList = 
    new Dictionary<string, List<Dictionary<string, int>>>();
myList.Add("SomeString", dictList);

I hope this helps.

这篇关于如何添加值到词典与里面的字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆