[英]How to parse strange json object from api in C#
我正在使用azure移動應用程序,它以我無法讀取的格式返回json數據。 使用以下方法獲取
var newMember = new Member() { Id = Settings.UserId };
var url = azureService.Client.MobileAppUri + ".auth/me";
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-ZUMO-AUTH", Settings.AuthToken);
var response = await client.GetAsync(new Uri(url));
response.EnsureSuccessStatusCode();
dynamic responseBody = await response.Content.ReadAsStringAsync();
responseBody為
"[
{
\"id_token\": \"tokenstring\",
\"provider_name\": \"aad\",
\"user_claims\": [
{
\"typ\": \"exp\",
\"val\": \"903i4231\"
},
{
\"typ\": \"nbf\",
\"val\": \"123516345294\"
},
{
\"typ\": \"ver\",
\"val\": \"1.0\"
},
{
\"typ\": \"iss\",
\"val\": \"https: \\\/\\\/login.microsoftonline.com\\\/somestring\\\/v2.0\\\/\"
},
{
\"typ\": \"http: \\\/\\\/schemas.xmlsoap.org\\\/ws\\\/2005\\\/05\\\/identity\\\/claims\\\/nameidentifier\",
\"val\": \"someotherstring\"
},
{
\"typ\": \"aud\",
\"val\": \"anotherstringstill\"
},
{
\"typ\": \"nonce\",
\"val\": \"stringy\"
},
{
\"typ\": \"iat\",
\"val\": \"3543345\"
},
{
\"typ\": \"http: \\\/\\\/schemas.microsoft.com\\\/ws\\\/2008\\\/06\\\/identity\\\/claims\\\/authenticationinstant\",
\"val\": \"6363456345\"
},
{
\"typ\": \"http: \\\/\\\/schemas.xmlsoap.org\\\/ws\\\/2005\\\/05\\\/identity\\\/claims\\\/givenname\",
\"val\": \"FIRSTNAME?\"
},
{
\"typ\": \"http: \\\/\\\/schemas.xmlsoap.org\\\/ws\\\/2005\\\/05\\\/identity\\\/claims\\\/surname\",
\"val\": \"LastName?\"
},
{
\"typ\": \"http: \\\/\\\/schemas.microsoft.com\\\/identity\\\/claims\\\/identityprovider\",
\"val\": \"google.com\"
},
{
\"typ\": \"http: \\\/\\\/schemas.microsoft.com\\\/identity\\\/claims\\\/objectidentifier\",
\"val\": \"somestringelse\"
},
{
\"typ\": \"emails\",
\"val\": \"address@gmail.com\"
},
{
\"typ\": \"tfp\",
\"val\": \"B2C_1_ScoreSignupIn\"
}
],
\"user_id\": \"useridstring\"
}
]"
如何將其轉換為有用的C#對象,以便獲得字符串Firstname?
和LASTNAME?
我試過了
string firstName = responseJson.claims.givenname;
無濟於事。 另外,這種JSON稱為什么類型。 我記得在學習azure API時曾讀過它,但是我不記得在哪里。 我什至不知道該怎么稱呼它。 同樣在JSON prettyprints jsonprettyprint.com但使用我不能將它轉換為C#對象http://json2csharp.com/
您可以安裝Newtonsoft.Json ,然后執行以下操作以從JSON中查找值
string jsn = File.ReadAllText("YourJSON.txt");
List<RootObject> ro = JsonConvert.DeserializeObject<List<RootObject>>(jsn);
foreach(UserClaim uc in ro[0].user_claims)
{
if(uc.val=="FIRSTNAME")
{
//Do whatever you want.
}
//or
if(uc.typ.Contains("givenname"))
{
Console.WriteLine(uc.val);
}
}
這將是您的JSON的類
public class UserClaim
{
public string typ { get; set; }
public string val { get; set; }
}
public class RootObject
{
public string id_token { get; set; }
public string provider_name { get; set; }
public List<UserClaim> user_claims { get; set; }
public string user_id { get; set; }
}
請嘗試這個
JavaScriptSerializer serializer = new JavaScriptSerializer();
dynamic item = serializer.Deserialize<object>("{ \"test\":\"some data\" }");
string test= item["test"];
取代這個
“ {\\” test \\“:\\”一些數據\\“}”
與您的JSON字符串。
您還可以指定確切的數據類型,而不是對象和動態變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.