【发布时间】:2016-12-14 14:49:16
【问题描述】:
我正在使用一个 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 时读过它,但我不记得在哪里。我什至不知道该怎么称呼它来搜索它。 jsonprettyprint.com 处的 json 漂亮打印,但我无法使用 http://json2csharp.com/ 将其转换为 C# 对象
【问题讨论】:
-
实际消息是有效的 json 我更改了那里的值,因为它在我被删除的地方到处都是令牌。我会尝试再次编辑它,以便 json 有效。
-
您无法转换为解析此 json 并将其转换为对象,对吧?
-
当我试图从中删除私有数据时,这个特定的对象被破坏了。但是,编译器在解析来自服务器的响应时不会抱怨。我只是不明白如何从对象中获取数据。
-
使用 Newtonsoft Nuget 包并尝试@Mohit 作为答案发布的方式。
-
成功了,谢谢。我仍然不明白这个 json 的格式。是所有
/转义字符还是什么?