c# - Cannot deserialize the current JSON object, WHY? -


i'm trying use webapi list of employees data base, using code: code of client mvc application:

        string u = "http://localhost:1411/api/employeeapi";         uri uri = new uri(u);          httpclient httpclient = new httpclient();          task<httpresponsemessage> response = httpclient.getasync(uri);          task.waitall(response);          httpresponsemessage resposta = response.result;          var msg = resposta.content.readasstringasync().result;          employee[] employees = jsonconvert.deserializeobject<employee[]>(msg);          return view(employees); 

and code of webapi:

    public ienumerable<employee> getemployees()     {         return db.employees.asenumerable();     } 

but error keeps popping , can't understand why:

cannot deserialize current json object (e.g. {"name":"value"}) type 'dataaccess.employee[]' because type requires json array (e.g. [1,2,3]) deserialize correctly. fix error either change json json array (e.g. [1,2,3]) or change deserialized type normal .net type (e.g. not primitive type integer, not collection type array or list) can deserialized json object. jsonobjectattribute can added type force deserialize json object. path 'message', line 1, position 11.

my employee class:

namespace dataaccess {     using system;     using system.collections.generic;      public partial class employee     {         public employee()         {             this.products = new hashset<product>();         }          public int employeeid { get; set; }         public string title { get; set; }         public string firstname { get; set; }         public string middlename { get; set; }         public string lastname { get; set; }         public byte[] rowguid { get; set; }         public system.datetimeoffset modifieddate { get; set; }          public virtual icollection<product> products { get; set; }     } } 

the json output i'm not sure how it

the msg variable content:

my msg variable returns

"{\"message\":\"an error has occurred.\",\"exceptionmessage\":\"the 'objectcontent`1' type failed serialize response body content type 'application/json; charset=utf-8'.\",\"exceptiontype\":\"system.invalidoperationexception\",\"stacktrace\":null,\"innerexception\":{\"message\":\"an error has occurred.\",\"exceptionmessage\":\"self referencing loop detected type 'system.data.entity.dynamicproxies.productsubcategory_9ec9a3706390de6a3b51f713f0ddac2162afb5b3fab8f8587c9a865333a7729a'. path '[0].products[0].productsubcategory.productcategory.productsubcategories'.\",\"exceptiontype\":\"newtonsoft.json.jsonserializationexception\",\"stacktrace\":\" @ newtonsoft.json.serialization.jsonserializerinternalwriter.checkforcircularreference(jsonwriter writer, object value, jsonproperty property, jsoncontract contract, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializelist(jsonwriter writer, iwrappedcollection values, jsonarraycontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializelist(jsonwriter writer, iwrappedcollection values, jsonarraycontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializeobject(jsonwriter writer, object value, jsonobjectcontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializelist(jsonwriter writer, iwrappedcollection values, jsonarraycontract contract, jsonproperty member, jsoncontainercontract collectioncontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serializevalue(jsonwriter writer, object value, jsoncontract valuecontract, jsonproperty member, jsoncontainercontract containercontract, jsonproperty containerproperty)\r\n @ newtonsoft.json.serialization.jsonserializerinternalwriter.serialize(jsonwriter jsonwriter, object value)\r\n @ newtonsoft.json.jsonserializer.serializeinternal(jsonwriter jsonwriter, object value)\r\n @ system.net.http.formatting.jsonmediatypeformatter.<>c__displayclassd.b__c()\r\n @ system.threading.tasks.taskhelpers.runsynchronously(action action, cancellationtoken token)\"}}"

the best way stop error occuring is:

in webapi project go app_start folder , add webapiconfig 4 lines of code:

config.enablesystemdiagnosticstracing(); config.formatters.jsonformatter.serializersettings.preservereferenceshandling = preservereferenceshandling.none; config.formatters.jsonformatter.serializersettings.referenceloophandling = referenceloophandling.ignore; config.formatters.jsonformatter.serializersettings.formatting = formatting.indented; config.formatters.remove(config.formatters.xmlformatter); 

and make sure have instaled json nuget package


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -