Nv1mvvCross 응용 프로그램 샘플의 Lesson 6을 통해 간단한 응용 프로그램을 만들려고합니다. 그러나 은 json 데이터을 serialization하는 동안 SimpleRestService에서 실패했습니다.JSON의 MvvmCross : Deserilization 오류
private T Deserialize<T>(string responseBody)
{ // Error is here for deserilizing
var toReturn = _jsonConverter.DeserializeObject<T>(responseBody);
return toReturn;
}
내 JSON 브라우저 통한 데이터 :
[{ "제품 설명": "전체", "ID": "0"}, { "제품 설명": "지정", "ID" : "Desc": "Desc": "진행중", "Id": "3"}, { "Desc": "해결 된", "Id": "4"}, { "Desc": " ","Id ":"5 "}, {"Desc ":"보유 ","Id ":"6 "}, {"Desc ":"저 ","Id ":"8 "}, {"Desc ":"대기 승인 ","Id ":"9 "}, {"Desc ":"취소됨 ","Id ":"10 "}, {"Desc ":"해결되지 않음 ","Id ":"8 "}]
내 Json 데이터가 응답기에 있음 :
[모두 \ ", \"모두 \ ", \"0 \ "}, \"설명 \ ": \"할당 됨 \ ", \"ID \ ": \" "진행중", "ID": \ "3 \"}, {\ "설명 \": \ "해결 된 \", \ "ID \", ""\ "설명 \": \ "닫힘 \", \ "ID \": \ "5 \"}, \ "보유 \", \ " "\"설명 \ ": \"낮은 \ ", \"ID \ ": \"8 \ "}, {\"설명 \ ": \"대기 승인 \ " , \ "Id \": \ "9 \"}, {\ "Desc \": \ "취소됨 \", \ "Id \": \ "10 \"}, {\ "Desc \": \ " \ ", \"ID \ 해결 ": \"8 \ "}]
오류 메시지가 표시 등 :
{Newtonsoft.Json.JsonSerializationException : JSON 배열을 직렬화 할 수 없습니다 (즉, [1,2,3])을 'Book.Core.Services.BookSearchResult'유형으로 변환합니다. deserialized 형식은 배열이거나 IEnumerable, ICollection 또는 IList와 같은 컬렉션 인터페이스를 구현해야합니다. JSON 배열을 강제로 deserialize하려면 JsonArrayAttribute를 유형에 추가하십시오. Path ', 1 행, 1 위치에 있습니다. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract) [0x00000] in : 0 Newtonsoft.Json.Serialization.JsonProperty 멤버, System.Object existingValue, System.String 참조 (Newtonsoft.Json.JsonReader 리더, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract 계약, Newtonsoft.Json.Serialization.JsonProperty 멤버) [0x00000] in : 0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader 리더, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract 계약, Newtonsoft.Json.Serialization.JsonProperty 멤버, Newtonsoft .Json.Serialization.JsonContainerContract containerContract, System.Object existingValue) [0x00000 ]에서 : 0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty (Newtonsoft.Json.JsonReader 판독기, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract 계약, Newtonsoft.Json.JsonConverter 변환기, Newtonsoft.Json.Serialization. JsonContainerContract containerContract)은 0x00000]에서 0 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader 리더 System.Type objectType의)에서 [은 0x00000]에서 0에서 Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft .Json.JsonReader 리더, System.Type objectType) [0x00000] in : 0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader 리더, System.Type objectType) [0x00000] in : 0 등 ..}
내 코드 부분 : 클래스 선언 :
public class BookSearchItem
{
public string Desc { get; set; }
public string Id { get; set; }
}
public class BookSearchResult
{
public List<BookSearchItem> items { get; set; }
}
바인딩 선언 : 공통 들어
public void StartSearchAsync(string whatFor, Action<BookSearchResult> success, Action<Exception> error)
{
string address = string.Format("http://192.168.0.76/eFACiLiTYPhone/MobileService/WinPhoneWCFService.svc/callstatustesting");
_simpleRestService.MakeRequest<BookSearchResult>(address,"GET", success, error);
}
간단한 레크리에이션 서비스 :
public class SimpleRestService :ISimpleRestService
{
private readonly IMvxJsonConverter _jsonConverter;
public SimpleRestService(IMvxJsonConverter jsonConverter)
{
_jsonConverter = jsonConverter;
}
public void MakeRequest<T>(string requestUrl, string verb, Action<T> successAction, Action<Exception> errorAction)
{
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Method = verb;
request.Accept = "application/json";
MakeRequest(
request,
(response) =>
{
if (successAction != null)
{
T toReturn;
try
{
toReturn = Deserialize<T>(response);
}
catch (Exception ex)
{
errorAction(ex);
return;
}
successAction(toReturn);
}
},
(error) =>
{
if (errorAction != null)
{
errorAction(error);
}
}
);
}
private void MakeRequest(HttpWebRequest request, Action<string> successAction, Action<Exception> errorAction)
{
request.BeginGetResponse(token =>
{
try
{
using (var response = request.EndGetResponse(token))
{
using (var stream = response.GetResponseStream())
{
var reader = new StreamReader(stream);
successAction(reader.ReadToEnd());
}
}
}
catch (WebException ex)
{
Mvx.Error("ERROR: '{0}' when making {1} request to {2}", ex.Message, request.Method, request.RequestUri.AbsoluteUri);
errorAction(ex);
}
}, null);
}
private T Deserialize<T>(string responseBody)
{
var toReturn = _jsonConverter.DeserializeObject<T>(responseBody);
return toReturn;
}
}
감사합니다. 친절하게 내 업데이트 된 샘플 코드를 찾으십시오. 나는 당신이 제안한대로 사용했습니다. 명확히하십시오. – Kathir