Json .Net은 플랫 객체를 복잡한 객체로 직렬화합니다 (직렬화 / 역 직렬화에 대한 객체 구조 변경).

알렉스 아트.

이런 플랫 DTO가 있습니다.

public class User
{
    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("fname")]
    public string FirstName { get; set; }

    [JsonProperty("lname")]
    public string LastName { get; set; }

    [JsonProperty("phone")]
    public string Phone { get; set; }

    [JsonProperty("city")]
    public string City { get; set; }

    [JsonProperty("country")]
    public string CountryCode { get; set; }

    [JsonProperty("state")]
    public string State { get; set; }

    [JsonProperty("zip")]
    public string Zip { get; set; }

    [JsonProperty("address1")]
    public string Address1 { get; set; }

    [JsonProperty("address2")]
    public string Address2 { get; set; }
}

기본적으로 '플랫'JSON으로 직렬화됩니다.

{
     'email':'[email protected]',
     'fname':'John',
     'phone':'123456789',
     'city':'New York',
     'zip':'1111',
     'lname':'Joe',
     'state':'NY',
     'address1' : 'address1'
}

더 구조화 된 JSON 객체로 직렬화하고 싶습니다.

{
     'email':'[email protected]',
     'fname':'John',
     'phone':'123456789',
     'lname':'Joe',
     'address' : {
         'city':'New York',
         'zip':'1111',         
         'state':'NY',
         'address1' : 'address1'
      }         
}

사용자 지정 JsonConverter를 만들지 않고 수행 할 수있는 방법이 있습니까?

다린 디미트로프

아니요, 사용자 지정 JsonConverter없이 또는 적절한 클래스 를 도입 하여 플랫 모델 의 구조를 추가하지 않고는이를 수행 할 수있는 방법이 없습니다 Address.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관