.net 4.0을 사용하는 mvc 프로젝트에서 ReadAsAsync ()를 사용하고 싶습니다. 결과는 null로 표시됩니다.
주소 표시 줄에 URI를 입력하면 다음과 같은 크롬 결과가 나타납니다 (태그 이름이 변경됨).
<ns2:MyListResponse xmlns:ns2="blablabla">
<customerSessionId>xxcustomerSessionIdxx</customerSessionId>
<numberOfRecordsRequested>0</numberOfRecordsRequested>
<moreResultsAvailable>false</moreResultsAvailable>
<MyList size="1" activePropertyCount="1">
<MySummary order="0">
<id>1234</id>
<name>...</name>
.
.
</MySummary>
</MyList>
</ns2:MyListResponse>
코드에서 문을 사용하면 :
using (var client = new HttpClient())
{
var response = client.GetAsync(apiUri).Result;
var message = response.Content.ReadAsStringAsync().Result;
var result1 = JsonConvert.DeserializeObject<MyListResponse>(message);
var result2 = response.Content.ReadAsAsync<MyListResponse>().Result;
}
메시지는 다음과 같이 "{\"MyListResponse\":{\"customerSessionId\"...}"
json 객체에 해당하는 문자열 형식으로 제공됩니다.
{"MyListResponse":
{"customerSessionId":"xxcustomerSessionIdxx",
"numberOfRecordsRequested":0,
"moreResultsAvailable":false,
"MyList":
{"@size":"1",
"@activePropertyCount":"1",
"MySummary":
{"@order":"0",
"id":1234,
"name":"...",
.
.
}
}
}
}
그리고 result1 및 result2의 속성은 null 또는 기본값으로 제공됩니다. 클래스 정의는 다음과 같습니다. 내용을 객체로 읽고 싶지만 읽을 수 없습니다. 이 문제를 해결하기 위해 무엇을 조언합니까? 내가 무엇을 잘못하고 있지? 미리 감사드립니다.
public class MySummary
{
public int @Order { get; set; }
public string Id { get; set; }
public string Name { get; set; }
.
.
}
public class MyList
{
public int @Size { get; set; }
public int @ActivePropertyCount { get; set; }
public MySummary MySummary{ get; set; }
}
public class MyListResponse
{
public string CustomerSessionId { get; set; }
public int NumberOfRecordsRequested { get; set; }
public bool MoreResultsAvailable { get; set; }
public MyList MyList { get; set; }
}
새 클래스를 다음과 같이 정의했습니다.
public class ResponseWrapper
{
public MyListResponse MyListResponse { get; set; }
}
이 래퍼와 함께 사용했습니다.
var result1 = JsonConvert.DeserializeObject<ResponseWrapper>(message);
var result2 = response.Content.ReadAsAsync<ResponseWrapper>().Result;
그런 다음 작동했습니다. MySummary 개체 만 필요하지만 작동하도록 더 많은 클래스를 작성해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다