JSON API 응답을 역 직렬화하려고하는데 자식 개체의 속성에 액세스하는 방법을 잘 모르겠습니다.
다음은 작업중인 API 응답의 예입니다.
{
"token_type": "Bearer",
"expires_at": 1598830199,
"expires_in": 21408,
"refresh_token": "*Removed*",
"access_token": "*Removed*",
"athlete": {
"id": *Removed*,
"username": null,
"resource_state": 2,
"firstname": "Jon",
"lastname": "Clyde",
"city": "*Removed*",
"state": "England",
"country": "United Kingdom",
"sex": "M",
"premium": true,
"summit": true,
"created_at": "*Removed*",
"updated_at": "*Removed*",
"badge_type_id": 1,
"profile_medium": "avatar/athlete/medium.png",
"profile": "avatar/athlete/large.png",
"friend": null,
"follower": null
}
}
expires_at, expires_in, access_token, refresh_token 속성이있는 클래스를 만든 후 다음 코드 줄을 사용하여 필요한 루트 속성에 액세스 할 수있었습니다.
using var responseStream = await response.Content.ReadAsStreamAsync();
var stravaResponse = await JsonSerializer.DeserializeAsync<Authorisation>(responseStream);
그러나 "athelete"의 자식 개체 아래에있는 id, username, firstname 및 lastname에 액세스해야합니다.
이것을 달성하는 좋은 권장 방법이 있습니까?
의 모델을 나타내는 다른 클래스를 추가 athlete
한 다음이를 최상위 클래스의 속성으로 만듭니다.
public class Authorisation
{
// Root properties
// Nested athlete object
public Athlete Athlete { get; set; }
}
public class Athlete
{
public string Id { get; set; }
public string Username { get; set; }
// Other properties
}
그런 다음 C #에서 평소와 같이 값에 액세스 할 수 있습니다.
var athleteId = stravaResponse.Athlete.Id;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다