从对象字典中获取值 C#

2023-12-13

我正在使用 LiveConnect sdk 来获取一些用户信息。
在做了一切必要的事情之后,这就是我得到的结果:

{
   "id": "123456789", 
   "name": "a b", 
   "first_name": "a", 
   "last_name": "b", 
   "link": "https://profile.live.com/", 
   "work": [], 
   "gender": null, 
   "emails": {
      "preferred": "[email protected]", 
      "account": "[email protected]", 
      "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"
}  

我需要得到"account" inside "emails".
首先,当我得到这个字符串时,我做了以下操作:

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

现在,为了获取帐户电子邮件,我要做的事情如下:

string email = userData["emails"]["account"];  

但由于这是一个字符串对象字典,我收到一个错误,指出这是不可能的,因为 userData["emails"] 是一个对象。

我如何获取数据?


你尝试过选角吗?

例如:

(userData["emails"] as Dictionary<string,object>)["account"]

or:

((Dictionary<string,object>)userData["emails"])["account"]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从对象字典中获取值 C# 的相关文章

随机推荐