簡體   English   中英

從對象字典C#獲取價值

[英]Getting value from object dictionary C#

我正在使用LiveConnect SDK獲取一些用戶信息。
在做完所有必要的事情之后,這就是我得到的結果:

{
   "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"獲取"account" "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"]

要么:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM