Reputation: 2079
I am working with an azure mobile app and it returns json data with a format I cannot read. The sting is acquired using
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 is
"[
{
\"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\": \"[email protected]\"
},
{
\"typ\": \"tfp\",
\"val\": \"B2C_1_ScoreSignupIn\"
}
],
\"user_id\": \"useridstring\"
}
]"
How can I convert this to a useful C# object so I can get the string Firstname?
and LASTNAME?
I tried
string firstName = responseJson.claims.givenname;
to no avail. Also, what is this type of JSON called. I remember reading about it while learning about azure API but I cannot remember where. I don't even know what to call it to search it up. Also the json prettyprints at jsonprettyprint.com but I cannot convert it to a C# object using http://json2csharp.com/
Upvotes: 0
Views: 1014
Reputation: 2047
Please try this
JavaScriptSerializer serializer = new JavaScriptSerializer();
dynamic item = serializer.Deserialize<object>("{ \"test\":\"some data\" }");
string test= item["test"];
Replace this
"{ \"test\":\"some data\" }"
with your JSON string.
You can also specify the exact data type instead of object and dynamic variables.
Upvotes: 0
Reputation: 14044
You can Install-Package Newtonsoft.Json and then this is what you can do to find the values from the 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);
}
}
This will be the classes for your 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; }
}
Upvotes: 3