简体   繁体   English

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

[英]How to get value from dictionary without knowing key?

I want to get value from dictionary but i don't know key(Because dynamic generate dictionary from database) how can i get dictionary value. 我想从字典中获取价值但我不知道密钥(因为动态从数据库生成字典)如何获得字典值。

If you some idea share me ...  

For Example my database string value like 例如我的数据库字符串值如

 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 is dynamic key and after it's value title all value i need FB是动态密钥,在它的价值标题之后我需要的所有价值

If you don't have the key, but have the value and trying to get hold of the key, you can do this: 如果你没有密钥,但有价值并试图抓住密钥,你可以这样做:

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

To get a sequence of all the Key/Value pairs where the value matches a target: 要获取值与目标匹配的所有键/值对的序列:

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);

The sample data you posted isn't a flat key-value dictionary. 您发布的示例数据不是平键值字典。 It contains embedded dictionaries - the base dictionary contains a title and a rows, which in turn consists of height and columns and so on, and at some point are key-value pairs who keys are, confusingly, named 'value'. 它包含嵌入式字典 - 基本字典包含标题和行,而行又由高度和列组成,依此类推,在某些时候是键值对,令人困惑地称为“值”。 Are you asking how to parse this data structure to get all the values whose key is value ? 您是否在询问如何解析此数据结构以获取其键值有效的所有value

What you first need to do, since this appears to be a JSON-formatted entry, is parse the JSON into a .NET data structure, using libraries like JSON.NET or System.Web.Helpers.Json . 您首先需要做的是,因为它似乎是一个JSON格式的条目,它使用JSON.NET或System.Web.Helpers.Json等库将JSON解析为.NET数据结构。 These libraries will convert the JSON string into a hierarchy of dictionaries, all of them implementing IEnumerable, so you can iterate over it, more or less like this (this is not compilable code, just a demonstration!): 这些库将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);
    }
}

This C#-ish pseudo-code shows you how to go over a dictionary, then optionally drill down deeper into internal dictionaries. 这个C#-ish伪代码向您展示如何查看字典,然后可以选择深入研究内部字典。

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();

How can you possibly know which value you need if you don't have the key? 如果没有密钥,怎么可能知道你需要哪个值?

A Dictionary in .NET does contain a Keys and Values collection, so if you are only interested in the values, you can use that. .NET中的Dictionary确实包含KeysValues集合,因此如果您只对值感兴趣,可以使用它。

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

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