从对象字典获取值C# [英] Getting value from object dictionary C#
问题描述
我正在使用LiveConnect sdk获取一些用户信息。
在做了必要的操作后,这是我得到的结果:
I am using the LiveConnect sdk to get some user info.
After doing whatever is necessary for that, this is the result I'm getting:
{
"id": "123456789",
"name": "a b",
"first_name": "a",
"last_name": "b",
"link": "https://profile.live.com/",
"work": [],
"gender": null,
"emails": {
"preferred": "a@live.com",
"account": "a@live.com",
"personal": null,
"business": null
},
"addresses": {
"personal": {
"street": null,
"street_2": null,
"city": null,
"state": null,
"postal_code": null,
"region": null
},
"business": {
"street": null,
"street_2": null,
"city": null,
"state": null,
"postal_code": null,
"region": null
}
},
"locale": "en_US",
"updated_time": "2013-10-10T08:41:14+0000"
}
我需要获取帐户
在emails
中。
首先,当我得到这个字符串时,我做了以下:
I need to get the "account"
inside "emails"
.
First, when I got this string I did the following:
public Dictionary<string, object> userData = new Dictionary<string, object>();
userData = deserializeJsonObject(<the string above>);
private Dictionary<string, object> deserializeJsonObject(string json)
{
var jss = new JavaScriptSerializer();
var d = jss.Deserialize<Dictionary<string, object>>(json);
return d;
}
现在,为了获得帐户电子邮件,我要做点事如:
Now, in order to get the account email, I was going to do something like:
string email = userData["emails"]["account"];
但由于这是一个字符串,对象字典,我收到一个错误,这是不可能的,因为userData [ 电子邮件]是一个对象。
but since this is a string, object dictionary, I get an error that it's not possible because userData["emails"] is an object.
如何获取数据?
推荐答案
试图做一个演员?
例如:
(userData [emails ] as Dictionary< string,object>)[account]
或:
((字典< string,object>)userData [emails])[account]
这篇关于从对象字典获取值C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!