我正在从其他api下载json字符串,并希望将其解析为我的对象,但我收到以下错误:
{Newtonsoft.Json.JsonSerializationException:转换值时出错“[{”Id“:1,”名称“:”演示iManage School“,”ApiUrl“:”http://demo.imanage-school.com/api/user/authenticatedUser/“,”LogoBytes“:null} ,{“Id”:2,“Name”:“Al Omam International School”,“ApiUrl”:“http://alomam.imanage-school.com/api/user/authenticatedUser/”,“LogoBytes”:null}]“输入'System.Collections.Generic.List { {1}} 1 [iManage.Models.SchoolModel]。 at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(System.Object value,System.Type initialType,System.Type targetType)[0x00067] in< 90125bc3858247a4a5e3af0c3035e4aa&gt ;:0 at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(System.Object initialValue,System.Globalization.CultureInfo culture,System.Type targetType)[0x00031] in< 90125bc3858247a4a5e3af0c3035e4aa&gt ;:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(Newtonsoft.Json.JsonReader reader,System.Object value,System.Globalization.CultureInfo culture,Newtonsoft.Json.Serialization.JsonContract contract,System.Type targetType)[0x0008d] in< 90125bc3858247a4a5e3af0c3035e4aa>:0
我在PCL中的服务代码:
1[iManage.Models.SchoolModel]'. Path '', line 1, position 288. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List
模型对象:
public void GetFeedItems(Action<List<SchoolModel>> success, Action<Exception> error)
{
var url = "http://demo.imanage-school.com/api/configurations/schoolnames";
var request = (HttpWebRequest)WebRequest.Create(url);
try
{
request.BeginGetResponse(result => ProcessResponse(success, error, request, result), null);
}
catch (Exception exception)
{
error(exception);
}
}
private void ProcessResponse(Action<List<SchoolModel>> success, Action<Exception> error, HttpWebRequest request, IAsyncResult result)
{
try
{
var response = request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
var text = reader.ReadToEnd();
var objects = JsonConvert.DeserializeObject<List<SchoolModel>>(text);
success(objects);
}
}
catch (Exception exception)
{
error(exception);
}
}
我的json:
[{“Id”:1,“Name”:“Demo iManage School”,“ApiUrl”:“http://demo-school.com/api/user/authenticatedUser/”,“LogoBytes”:null},{“Id”:2,“Name” :“Al Omam国际学校”,“ApiUrl”:“http://demo-school.com/api/user/authenticatedUser/”,“LogoBytes”:null}]
我正在使用NewtonSoft进行解析。
答案 0 :(得分:0)
错误是因为json响应周围有额外的双引号。我删除了它,它开始工作。