如何在不知道密钥的情况下从字典获取价值?

时间:2014-03-19 12:34:19

标签: c# c#-4.0

我想从字典中获取价值,但我不知道密钥(因为动态生成数据库中的字典)如何才能获得字典值。

If you some idea share me ...  

例如我的数据库字符串值,如

 string jsonString =  " "FB": "[{\"title\":\"sheet1\",\"rows\":[{\"height\":\"undefined\",\"columns\":[{\"value\":\"Cover Group \"},{\"value\":\"Sample Variable\"},{\"value\":\"Coverpoint Name\"},{\"value\":\"Crossed cover points\"},{\"value\":\"Coverpoint Comment\"},{\"value\":\"Bin Type\"},{\"value\":\"Bin Id\"},{\"value\":\"Sample Value\"},{\"value\":\"Expected Bin Count\"},{\"value\":\"Set Max Bin\"},{\"value\":\"Not Used\"}]},{\"height\":\"undefined\",\"columns\":[{\"value\":\"allCg,allSi\"},{\"value\":\"exSingle\"},{\"value\":\"exSingle\"},{},{\"value\":\"Example for single bin\"},{\"value\":\"single\"},{\"value\":\"valZero\"},{\"value\":\"1'b0\"},{\"formula\":\"1\",\"value\":1},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{\"value\":\"single\"},{\"value\":\"valOne\"},{\"value\":\"1'b1\"},{\"formula\":\"1\",\"value\":1},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex1Bus[3:0]\"},{\"value\":\"exMulti\"},{},{\"value\":\"Example for multibin\"},{\"value\":\"multi\"},{},{\"value\":\"[0:15]\"},{\"formula\":\"16\",\"value\":16},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{\"value\":\"exCross\"},{\"value\":\"exSingle,exMulti\"},{\"value\":\"Example for cross\"},{\"value\":\"Implicit\"},{},{},{\"formula\":\"32\",\"value\":32},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex2Bus[15:0]\"},{\"value\":\"exWildcard\"},{},{\"value\":\"example for wildcard\"},{\"value\":\"wildcard\"},{\"value\":\"ex_wildcard\"},{\"value\":\"16'bxxxxxxxxxxxxxx1\"},{\"formula\":\"1\",\"value\":1},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex3Bus[4:0]\"},{\"value\":\"exImplicit\"},{},{\"value\":\"example for implicit & set max bin\"},{\"value\":\"Implicit\"},{},{},{\"formula\":\"8\",\"value\":8},{\"formula\":\"8\",\"value\":8},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex4Bus[3:0]\"},{\"value\":\"ex4Bus\"},{},{\"value\":\"setup for ignore example\"},{\"value\":\"multi\"},{},{\"value\":\"[0:15]\"},{\"formula\":\"16\",\"value\":16},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{\"value\":\"exIgnore\"},{\"value\":\"exSingle,ex4Bus\"},{\"value\":\"example for ignore\"},{\"value\":\"ignore\"},{},{\"value\":\"ex4Bus([12:15])\"},{\"formula\":\"24\",\"value\":24},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{\"value\":\"ex5Bus[3:0]\"},{\"value\":\"exIllegal\"},{},{\"value\":\"example for illegal\"},{\"value\":\"illegal\"},{},{\"value\":\"[12:15]\"},{\"formula\":\"16\",\"value\":16},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]},{\"height\":\"undefined\",\"columns\":[{},{},{},{},{},{},{},{},{},{},{}]}],\"metadata\":{\"widths\":[\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\",\"200\"],\"frozenAt\":{\"row\":0,\"col\":0}}}]""

FB是动态密钥,在它的值标题之后我需要的所有值

5 个答案:

答案 0 :(得分:2)

如果你没有密钥,但有价值并试图抓住密钥,你可以这样做:

Dictionary<string, string> testData = new Dictionary<string, string>();
testData.Add("name", "Latheesan");
testData.Add("age", "26");

KeyValuePair<string, string> searchResult 
    = testData.FirstOrDefault(s => s.Value == "Latheesan");

string key = searchResult.Key; // returns "name" here

答案 1 :(得分:1)

获取值与目标匹配的所有键/值对的序列:

var dict = new Dictionary<string, int>
{
    {"One", 1}, 
    {"Two", 2}, 
    {"Another One", 1}, 
    {"Three", 3}, 
    {"Yet Another One", 1}
};

int target = 1; // For example.
var matches = dict.Where(item => item.Value == target);

foreach (var kvp in matches)
    Console.WriteLine("Key = " + kvp.Key);

答案 2 :(得分:1)

您发布的示例数据不是平键值字典。它包含嵌入式词典 - 基本词典包含标题和行,而行又由高度和列组成,依此类推,在某些时候是键值对,这些键是令人困惑的,命名为&#39; value&#39 ;。您是否在询问如何解析此数据结构以获取其键为value的所有值?

首先需要做的是,因为这似乎是一个JSON格式的条目,它使用JSON.NET或System.Web.Helpers.Json之类的库将JSON解析为.NET数据结构。这些库将JSON字符串转换为字典层次结构,所有这些字典都实现了IEnumerable,因此您可以或多或少地迭代它(这是不是可编译代码,只是演示!) :

public void Main()
{
    var jsonObject = Json.Decode(FB); // FB is your JSON string.
    var values = new List<string>();
    FindValues(jsonObject);
}

public void FindValues(jsonObject, values)
{
    foreach (var child in jsonObject)
    {
          if (child.key == 'value')
          {
               values.Add(child.value);
          }

          // Recursively call FindValues on child objects.
          FindValues(child, values);
    }
}

这个C#-ish伪代码向您展示如何浏览字典,然后可选择深入研究内部字典。

答案 3 :(得分:1)

This code use for get value from dictionary value without knowing key and value..  

var json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonString );
var javSer = new JavaScriptSerializer();
var dfi = javSer.Deserialize<Dictionary<string, string>>(json);
string dataString= dfi.Values.First();

答案 4 :(得分:-1)

如果您没有钥匙,怎么可能知道您需要哪个值?

.NET中的Dictionary确实包含KeysValues集合,因此如果您只对这些值感兴趣,可以使用它。