简体   繁体   English

无法将json字符串解析为xamarin中pcl文件中的对象

[英]Unable to parse json string to object in pcl file in xamarin

I am downloading json string from a rest api and want to parse it to my object but I am getting following error: 我正在从rest api下载json字符串,并想将其解析为我的对象,但出现以下错误:

{Newtonsoft.Json.JsonSerializationException: Error converting value "[{"Id":1,"Name":"Demo 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}]" to type 'System.Collections.Generic.List 1[iManage.Models.SchoolModel]'. Path '', line 1, position 288. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List {Newtonsoft.Json.JsonSerializationException:转换值“ [{” Id“:1,” Name“:” Demo 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[iManage.Models.SchoolModel]'. Path '', line 1, position 288. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List 1[iManage.Models.SchoolModel]'. Path '', line 1, position 288. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List 1[iManage.Models.SchoolModel]. 1[iManage.Models.SchoolModel]'. Path '', line 1, position 288. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List 1 [iManage.Models.SchoolModel]。 at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable (System.Object value, System.Type initialType, System.Type targetType) [0x00067] in <90125bc3858247a4a5e3af0c3035e4aa>:0 at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast (System.Object initialValue, System.Globalization.CultureInfo culture, System.Type targetType) [0x00031] in <90125bc3858247a4a5e3af0c3035e4aa>: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 在Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(0)上<90125bc3858247a4a5e3af0c3035e4aa>:0中的Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(System.Object value,System.Type initialType,System.Type targetType)[0x00067] ,System.Globalization.CultureInfo文化,System.Type targetType)[0x00031]在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(Newtonsoft.Json.JsonReaderReader读者,System.Object值,System.Global。)中<90125bc3858247a4a5e3af0c3035e4aa>:0中。 <90125bc3858247a4a5e3af0c3035e4aa>:0中的区域性,Newtonsoft.Json.Serialization.JsonContract合同,System.Type targetType)[0x0008d]

My service code in PCL: 我在PCL中的服务代码:

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);
        }
    }

Model object: 模型对象:

public class SchoolModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ApiUrl { get; set; }
    public byte[] LogoBytes { get; set; }
}

My json: 我的json:

[{"Id":1,"Name":"Demo iManage School","ApiUrl":" http://demo-school.com/api/user/authenticatedUser/ ","LogoBytes":null},{"Id":2,"Name":"Al Omam International School","ApiUrl":" http://demo-school.com/api/user/authenticatedUser/ ","LogoBytes":null}] [{“ Id”:1,“名称”:“ Demo iManage School”,“ ApiUrl”:“ http://demo-school.com/api/user/authenticatedUser/ ”,“ LogoBytes”:null},{“ Id“:2,”名称“:” Al Omam国际学校“,” ApiUrl“:” http://demo-school.com/api/user/authenticatedUser/ “,” LogoBytes“:null}]

I am using NewtonSoft for parsing. 我正在使用NewtonSoft进行解析。

The error was because of additional double quotes around the json response. 该错误是由于json响应周围有额外的双引号引起的。 I removed it and it started working. 我将其删除,然后开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM