jeudi 5 octobre 2017

Consuming asp.net web.api json not working

I'm trying to consume my web api which returns JSON format from my asp.net mvc web application. As my JSON format has root node (the way i filled with oledb dataset). I'm keep getting the following error.

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[FileAttributes]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'SearchData', line 1, position 14.

Here is the code sample

            string Baseurl = "http://localhost:62602/";
            client.BaseAddress = new Uri(Baseurl);
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage Res = await client.GetAsync("api/FileSearch/?searchTerm=" + searchTerm );
            if (Res.IsSuccessStatusCode)
            {
                var fsResponse = Res.Content.ReadAsStringAsync().Result;
                List<FileAttributes> fileAttribultes = JsonConvert.DeserializeObject<List<FileAttributes>>(fsResponse);
            }

And then following some ideas, I used JSON.Parse and SelectToken to remove root node, but this time the List returns data but with null vaues JToken jtok = JObject.Parse(fsResponse).SelectToken("SearchData"); List fileAttribultes = JsonConvert.DeserializeObject>(fsResponse);

Here is my FileAttributes class

public class FileAttributes
{

    public string Name { get; set; }
    public string Header { get; set; }
    public string Title { get; set; }
 }

Blockquote

I appreciate if anyone check whats wrong in the deserialization of JSON part with root node and whats the right way to do it?

Thanks Jaffar




Aucun commentaire:

Enregistrer un commentaire