2017-11-01 11 views
0

그리드 바운스 API를 보내기 모든 비어있는 날이 유사한 목록을 반환 : 그 JSON을 해제 직렬화하려고 다음과 같은 클래스를 사용올바른 JSON 직렬화 복원 후 개체 수 있지만,이

"[ 
    {\"created\":1487173664,\"email\":\"[email protected]\",\"reason\":\"550 No Such User Here \",\"status\":\"550\"} 
    ,{\"created\":1487169530,\"email\":\"[email protected]\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"} 
    ,{\"created\":1487095343,\"email\":\"[email protected]\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"} 
    ,{\"created\":1487093087,\"email\":\"[email protected]\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"} 
    ,{\"created\":1487085008,\"email\":\"[email protected]\",\"reason\":\"550 Requested action not taken: mailbox unavailable \",\"status\":\"550\"} 
    ,{\"created\":1487082934,\"email\":\"[email protected]\",\"reason\":\"550 Invalid recipient <[email protected]> (#5.1.1) \",\"status\":\"550\"} 
]" 

을 (함께 노력 및 직렬화와 같은 [DataMember] 장식없이 작동/O w이) 잘 :

[DataContract(Name = "SendGridBounce")] 
public class SendGridBounce 
{ 
    [DataMember] 
    public int Created { get; set; } 
    [DataMember] 
    public string Email { get; set; } 
    [DataMember] 
    public string Reason { get; set; } 
    [DataMember] 
    public string Status { get; set; } 
} 

[CollectionDataContract(Name = "SendGridBounceList")] 
public class SendGridBounceList : List<SendGridBounce> 
{ 
} 

그리고 이것은 내가 한 방법입니다 :

var client = new SendGridClient("some API key here"); 
string queryParams = String.Format(CultureInfo.InvariantCulture, "{{ 'end_time': {0}, 'start_time': 1 }}", DateTimeOffset.UtcNow.ToUnixTimeSeconds()); 
var response = Task.Run(() => client.RequestAsync(SendGridClient.Method.GET, urlPath: "suppression/bounces", queryParams: queryParams)).Result; 

DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(SendGridBounceList)); 
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(response.Body.ReadAsStringAsync().Result))) 
{ 
    SendGridBounceList bl = js.ReadObject(ms) as SendGridBounceList; 

    foreach (var b in bl) 
    { 
     tbxOutput.Text += b.Created.ToString() + ", " + b.Status + ", " + b.Email + ", " + b.Reason + Environment.NewLine; 
    } 
} 
,536,913,632 10

결과적으로 SendGrid에서 반환 한 JSON만큼 목록에있는 항목이 많지만 각 항목은 초기화되지 않음 {0, null, null, null}입니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

[DataContract(Name = "SendGridBounce")] 
public class SendGridBounce 
{ 
    [DataMember(Name ="created")] 
    public int Created { get; set; } 
    [DataMember(Name = "email")] 
    public string Email { get; set; } 
    [DataMember(Name = "reason")] 
    public string Reason { get; set; } 
    [DataMember(Name = "status")] 
    public string Status { get; set; } 
} 
+3

그것은 아마 당신의 속성 이름의 케이스입니다 다음과 같이 – Amy

+0

@Amy는 차이가 없습니다 – ajeh

+0

@ L.B 좀 더 정교 할 수 있습니까? – ajeh

답변

0

솔루션

DataMember 장식을 변경했다. 속성은 JSON에서 하위 사례입니다.