c# - Deserializing an array of objects with Json.Net -
the received data this:
inside each item, there object, customer
, have identical class that. how can convert them using json.net?
i have tried followings:
var data = jsonconvert.deserializeobject<list<customer>>(val);
and adding class:
public class customerjson { public customer customer{ get; set; } }
and trying deserialize it:
var data = jsonconvert.deserializeobject<list<customerjson>>(val);
with both of them exception:
cannot deserialize current json object (e.g. {"name":"value"}) type 'system.collections.generic.list`1[customer]' 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 'rows', line 1, position 8.
data:
{"rows":[{"id":"232333","name":"nam"},{"id":"3434444","name":"2ndname"}]}
if read json data structure correctly want this:
public class root { public list<customer> rows { get; set; } }
and
var data = jsonconvert.deserializeobject<root>(val);
tested code:
void main() { var test = jsonconvert.deserializeobject<root>("{\"rows\":[{\"id\":\"232333\",\"name\":\"nam\"},{\"id\":\"3434444\",\"name\":\"2ndname\"}]}"); console.writeline(test.rows[0].id); // prints 232333 } public class customer { public int id { get; set; } } public class root { public list<customer> rows { get; set; } }
Comments
Post a Comment