pankaj
pankaj

Reputation: 8378

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:

{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.List1[iManage.Models.SchoolModel]'. Path '', line 1, position 288. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List1[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

My service code in 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:

[{"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}]

I am using NewtonSoft for parsing.

Upvotes: 0

Views: 389

Answers (1)

pankaj
pankaj

Reputation: 8378

The error was because of additional double quotes around the json response. I removed it and it started working.

Upvotes: 0

Related Questions