我正在尝试通过N + 1天MvvmCross应用程序示例中的第6课创建一个简单的应用程序。但是,在转换json Data 序列化时,在SimpleRestService中失败。
private T Deserialize<T>(string responseBody)
{ // Error is here for deserilizing
var toReturn = _jsonConverter.DeserializeObject<T>(responseBody);
return toReturn;
}
我的Json数据通过浏览器:
[{“Desc”:“All”,“Id”:“0”},{“Desc”:“Assigned”,“Id”:“2”},{“Desc”:“In Progress”, “ID”: “3”},{ “商品说明”: “解决”, “ID”: “4”},{ “商品说明”: “关闭”, “ID”: “5”},{ “商品说明”: “Hold”,“Id”:“6”},{“Desc”:“低”,“Id”:“8”},{“Desc”:“等待批准”,“Id”:“9”}, {“Desc”:“已取消”,“Id”:“10”},{“Desc”:“未解决”,“Id”:“8”}]
我在响应者的应用程序中的Json数据:
[{\ “DESC \”:\ “所有\”,\ “ID \”:\ “0 \”},{\ “DESC \”:\ “分配\”,\ “ID \”:\ “2 \”},{\“Desc \”:\“正在进行\”,\“Id \”:\“3 \”},{\“Desc \”:\“已解决”,\“Id \ “:\” 4 \ “},{\” DESC \ “:\” 封闭\ “\ ”ID \“:\ ”5 \“},{\ ”DESC \“:\ ”暂停\“,\” Id \“:\”6 \“},{\”Desc \“:\”低\“,\”Id \“:\”8 \“},{\”Desc \“:\”等待批准\“ ,\ “ID \”:\ “9 \”},{\ “DESC \”:\ “取消\”,\ “ID \”:\ “10 \”},{\ “DESC \”:\“不解决\”,\ “ID \”:\ “8 \”}]
错误消息显示为:
{Newtonsoft.Json.JsonSerializationException:无法将JSON数组(即[1,2,3])反序列化为类型'Book.Core.Services.BookSearchResult'。 反序列化类型必须是数组或实现集合接口,如IEnumerable,ICollection或IList。 要强制JSON数组反序列化,请将JsonArrayAttribute添加到该类型。路径'',第1行,第1位。 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(Newtonsoft.Json.JsonReader reader,System.Type objectType,Newtonsoft.Json.Serialization.JsonContract contract)[0x00000] in:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(Newtonsoft.Json.JsonReader reader,System.Type objectType,Newtonsoft.Json.Serialization.JsonContract contract,Newtonsoft.Json.Serialization.JsonProperty member,System.Object existingValue,System.String reference )[0x00000] in:0 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(Newtonsoft.Json.JsonReader reader,System.Type objectType,Newtonsoft.Json.Serialization.JsonContract contract,Newtonsoft.Json.Serialization.JsonProperty member,Newtonsoft.Json.Serialization.JsonContainerContract containerContract, System.Object existingValue)[0x00000] in:0 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueNonProperty(Newtonsoft.Json.JsonReader reader,System.Type objectType,Newtonsoft.Json.Serialization.JsonContract contract,Newtonsoft.Json.JsonConverter converter,Newtonsoft.Json.Serialization.JsonContainerContract containerContract)[0x00000在:0 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Newtonsoft.Json.JsonReader reader,System.Type objectType)[0x00000] in:0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal(Newtonsoft.Json.JsonReader reader,System.Type objectType)[0x00000] in:0 在Newtonsoft.Json.JsonSerializer.Deserialize(Newtonsoft.Json.JsonReader reader,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;
}
}
答案 0 :(得分:1)
您必须为Json调用使用正确的T
- 您不能简单地将BookSearchResult
用于所有Json调用。
您可以使用http://json2csharp.com/之类的工具为您生成CSharp类 - 例如
public class RootObject
{
public string Desc { get; set; }
public string Id { get; set; }
}
然后您可以将其用作:
var myItems = service.Deserialize<List<RootObject>>(jsonText);