c# - Deserialize json array in web api project -
c# - Deserialize json array in web api project -
my controller method follows
public actionresult index() { tweets model = null; var client = new httpclient(); var task = client.getasync("http://localhost:33615/api/product").continuewith((t) => { var response = t.result; var readtask = response.content.readasasync<tweets>(); readtask.wait(); model = readtask.result; }); task.wait(); homecoming view(model.result); }
the url homecoming row follws:
[{"id":1,"name":"honda civic","description":"luxury model 2013"},{"id":2,"name":"honda accord","description":"deluxe model 2012"},{"id":3,"name":"bmw v6","description":"v6 engine luxury 2013"},{"id":4,"name":"audi a8","description":"v8 engine 2013"},{"id":5,"name":"mercedes m3","description":"basic model 2013"}]
my tweets , tweet class follows:
public class tweets { public tweet[] result; } public class tweet { [jsonproperty("name")] public string name { get; set; } [jsonproperty("description")] public string description { get; set; } }
i can't figure out mistake.it gives me next error:
cannot deserialize current json array (e.g. [1,2,3]) type 'restfiddlertest.controllers.tweets' because type requires json object (e.g. {"name":"value"}) deserialize correctly
how prepare this.
i've double checked code , there few issues it. first of all, don't see logic deserialize json, according json object, i'm guessing you're using newtonsoft.json?
then have class tweets
contains array of tweet
objects. now, when create little sample, following:
var datatoserialize = new tweets(); datatoserialize.result = new [] { new tweet { description = "desc", name = "name" } }; var info = jsonconvert.serializeobject(datatoserialize);
the output of piece of code following:
{"result":[{"id":null,"name":"name","description":"desc"}]}
this not same output webapi.
now, when utilize newtonsoft.json, when serializing object, doing this:
const string json = "[{\"id\":1,\"name\":\"honda civic\",\"description\":\"luxury model 2013\"},{\"id\":2,\"name\":\"honda accord\",\"description\":\"deluxe model 2012\"},{\"id\":3,\"name\":\"bmw v6\",\"description\":\"v6 engine luxury 2013\"},{\"id\":4,\"name\":\"audi a8\",\"description\":\"v8 engine 2013\"},{\"id\":5,\"name\":\"mercedes m3\",\"description\":\"basic model 2013\"}]"; var info = jsonconvert.deserializeobject<tweets>(json);
note: i've set json string in string because didn't have time write total webapi.
now, not work, because string input , that's 1 controller homecoming not tweets
object (remember you're missing result
in json string? instead, array of tweet
objects, can alter code following:
var info = jsonconvert.deserializeobject<tweet>(json);
unfortunately, receive exact same error. why that? because you're trying deserialize object object of type tweet
while object array of type tweet
.
so, modify code following:
var info = jsonconvert.deserializeobject<tweet[]>(json);
now, you're deserializing object right type.
so, hope post help you.
kind regards,
c# json rest web-api
Comments
Post a Comment