帮助Dictonary [英] Help with Dictonary

查看:72
本文介绍了帮助Dictonary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从我的字典中提取结果:


 Dictionary< string,对象> dictionary = JsonConvert.DeserializeObject< Dictionary< string,object>>(Response); 
if(dictionary.Contains(" _id"))
{
string someVal = Convert.ToString(dictionary [" _id"]);
}

这是我正在构建字典的字符串:


" {\" result\":[{\" _id\":\" 6df53c39-cdd6-46ad-9cea-a1909560c24c\",\" _rev\":\" 0 \",\" userName \":\" uokgames@gmail.com\",\" mail\":\" uokgames@gmail.com\",\" givenName\":\" Micah\",\" sn\" ;: \" Holmes\",\" accountStatus\":\" Active\",\" avectraId\":\" 8022760434\" ;, \" AHApw\":\" GBVNaakn2BsIVLWb / inwneJueO16CkW6F2Fg7e3dnVEnoOwk5VyB2ZORtnM + 1Bxh\",\" regMethod\":\" ahaImport\",\" activationToken\\ \\":\"交流802f374ae5aec21b38453494dce7de\",\" activationTokenTS\":\" 1508184407183\",\" ahaMemberships\":[],\" access\":[ ]}],\" resultCount\":1,\" pagedResultsCookie\":空,\" totalPagedResultsPolicy\":\" NONE\",\ " totalPagedResults\": - 1,\" remainingPagedResults\": - 1 }"



如果它存在,我只需要得到_id值。


解决方案

JsonConvert是第三方库的一部分。您应该在论坛中发布与第三方库(如JSON.NET)相关的问题。


至于字典方法,请注意您的JSON实际上只有几个键。您想要的数据是结果下的数组。所以你必须获得结果的值然后处理它。但是因为你正在使用对象作为值,它将以
返回,然后你需要再次转换。


更简单的方法可能是让转换器弄清楚了类型。这大大简化了代码。当然,如果你需要多个值,那么你应该继续定义一个反序列化的类型。

 var value = JsonConvert.DeserializeObject(response)as JObject; 

//结果是一个数组,所以抓住第一个
var token = value [" result"] ?. ToArray()。FirstOrDefault();

var id =(令牌!= null)? token [" _id"]:null;



I'm trying to pull out a result from my dictionary:

Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(Response);
                                if (dictionary.Contains("_id"))
                                {
                                    string someVal = Convert.ToString(dictionary["_id"]);
                                }

Here is the string that I'm building my dictionary with:

"{\"result\":[{\"_id\":\"6df53c39-cdd6-46ad-9cea-a1909560c24c\",\"_rev\":\"0\",\"userName\":\"uokgames@gmail.com\",\"mail\":\"uokgames@gmail.com\",\"givenName\":\"Micah\",\"sn\":\"Holmes\",\"accountStatus\":\"Active\",\"avectraId\":\"8022760434\",\"AHApw\":\"GBVNaakn2BsIVLWb/inwneJueO16CkW6F2Fg7e3dnVEnoOwk5VyB2ZORtnM+1Bxh\",\"regMethod\":\"ahaImport\",\"activationToken\":\"ac802f374ae5aec21b38453494dce7de\",\"activationTokenTS\":\"1508184407183\",\"ahaMemberships\":[],\"access\":[]}],\"resultCount\":1,\"pagedResultsCookie\":null,\"totalPagedResultsPolicy\":\"NONE\",\"totalPagedResults\":-1,\"remainingPagedResults\":-1}"

I just need to get the _id value if it exists.

解决方案

JsonConvert is part of a third-party library. You should post questions related to third party libraries like JSON.NET in their forums.

As for the dictionary approach, notice that your JSON actually only has a couple of keys. The data you want is an array under result. So you'd have to get the value for result and then process it. But since you're using object as the value it'll come back as JSON that you'd then need to convert again.

The easier approach might be to let the converter figure out the type. This simplifies your code dramatically. Of course if you need more than a few values then you should go ahead and define a type to deserialize to instead.

var value = JsonConvert.DeserializeObject(response) as JObject;

//The result is an array so grab the first one
var token = value["result"]?.ToArray().FirstOrDefault();

var id = (token != null) ? token["_id"] : null;


这篇关于帮助Dictonary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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