mvvm - MvvmCross: Deserilization Error for JSON -


i'm trying create simple application through lesson 6 in n+1 days of mvvmcross application sample. failed in simplerestservice while converting json data serialization.

private t deserialize<t>(string responsebody) {   // error here deserilizing     var toreturn = _jsonconverter.deserializeobject<t>(responsebody);     return toreturn; } 

my json data through browser:

[{"desc":"all","id":"0"},{"desc":"assigned","id":"2"},{"desc":"in progress","id":"3"},{"desc":"resolved","id":"4"},{"desc":"closed","id":"5"},{"desc":"hold","id":"6"},{"desc":"低","id":"8"},{"desc":"waiting approval","id":"9"},{"desc":"cancelled","id":"10"},{"desc":"not resolved","id":"8"}]

my json data in application @ responsebody:

[{\"desc\":\"all\",\"id\":\"0\"},{\"desc\":\"assigned\",\"id\":\"2\"},{\"desc\":\"in progress\",\"id\":\"3\"},{\"desc\":\"resolved\",\"id\":\"4\"},{\"desc\":\"closed\",\"id\":\"5\"},{\"desc\":\"hold\",\"id\":\"6\"},{\"desc\":\"低\",\"id\":\"8\"},{\"desc\":\"waiting approval\",\"id\":\"9\"},{\"desc\":\"cancelled\",\"id\":\"10\"},{\"desc\":\"not resolved\",\"id\":\"8\"}]

error message shows :

{newtonsoft.json.jsonserializationexception: cannot deserialize json array (i.e. [1,2,3]) type 'book.core.services.booksearchresult'. deserialized type must array or implement collection interface ienumerable, icollection or ilist. force json arrays deserialize add jsonarrayattribute type. path '', line 1, position 1. @ newtonsoft.json.serialization.jsonserializerinternalreader.ensurearraycontract (newtonsoft.json.jsonreader reader, system.type objecttype, newtonsoft.json.serialization.jsoncontract contract) [0x00000] in :0 @ 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] in :0 @ newtonsoft.json.serialization.jsonserializerinternalreader.deserialize (newtonsoft.json.jsonreader reader, system.type objecttype) [0x00000] in :0 @ 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 etc.. }

my code part: class declaration:

public class booksearchitem {     public string desc { get; set; }     public string id { get; set; } }     public class booksearchresult {    public list<booksearchitem> items { get; set; }       } 

binding declaration:

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

simple rest service common:

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

you have use correct t json call - can't use booksearchresult json calls.

you can use tools http://json2csharp.com/ generate csharp classes - e.g.

public class rootobject {     public string desc { get; set; }     public string id { get; set; } } 

which can use as:

var myitems = service.deserialize<list<rootobject>>(jsontext); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -