c# - can not deserialize json list -
here json
result postman
{ "result": [ { "brands": [], "services": [], "addresses": [], "accesspoints": [], "kodu": "100", "name": "üeticissdss", "taxnumber": "asd", "taxoffice": "sada", "id": "bfb064f4-0732-43ab-9d94-87f6934258fe", "isactive": true }, { "brands": [], "services": [], "addresses": [], "accesspoints": [], "kodu": "100", "name": "üeticissdss", "taxnumber": "asd", "taxoffice": "sada", "id": "bfb064f4-0732-43ab-9d94-87f69d4258fe", "isactive": true } ], "id": 13, "exception": null, "status": 5, "iscanceled": false, "iscompleted": true, "iscompletedsuccessfully": true, "creationoptions": 0, "asyncstate": null, "isfaulted": false }
and trying deserialize json below :
[bindproperty] public list<manufacturer> manufacturer { get; set; } httpclient httpclient = new httpclient(); httpclient.baseaddress = new uri(apiparameters.httpclientbaseaddress); httpclient.defaultrequestheaders.accept.clear(); httpclient.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue(apiparameters.defaultrequestheadersjson)); customindexmodel.apiresponsemessage = await httpclient.getasync(apiparameters.httpclientbaseaddress); if (customindexmodel.apiresponsemessage.issuccessstatuscode) { var jsonasstring = customindexmodel.apiresponsemessage.content.readasstringasync().result; manufacturer = jsonconvert.deserializeobject<list<manufacturer>>(jsonasstring); }
the error message :
cannot deserialize current json object (e.g. {"name":"value"}) type 'system.collections.generic.list`1[q.entity.manufacturer.manufacturer]' 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 'result', line 1, position 10.
it looks i'm miss can not figure out. me please ?
this de c# class generated http://json2csharp.com/
public class result { public list<object> brands { get; set; } public list<object> services { get; set; } public list<object> addresses { get; set; } public list<object> accesspoints { get; set; } public string kodu { get; set; } public string name { get; set; } public string taxnumber { get; set; } public string taxoffice { get; set; } public string id { get; set; } public bool isactive { get; set; } } public class rootobject { public list<result> result { get; set; } public int id { get; set; } public object exception { get; set; } public int status { get; set; } public bool iscanceled { get; set; } public bool iscompleted { get; set; } public bool iscompletedsuccessfully { get; set; } public int creationoptions { get; set; } public object asyncstate { get; set; } public bool isfaulted { get; set; } }
and deserialize json datalist
[bindproperty] public rootobject datalist { get; set; } var jsonasstring = customindexmodel.apiresponsemessage.content.readasstringasync().result; datalist = jsonconvert.deserializeobject<rootobject>(jsonasstring);
after json deserialization able use result in razor page below
@foreach (var item in model.datalist.result) { ++rownumber; <tr> <td>@item.code</td> <td>@item.name</td> <td>@item.isactive</td> </
thank you.
Comments
Post a Comment