user18972374
user18972374

Reputation:

Can't deserialize json from Api

I'm trying to get data from my Api to display it on my Android Xamarin project.

My Api has JWT Authentication. I'm able to get the token and the response from my Api correct but when I have to deserialize it, either it won't be able to do it or it won't return anything.

Here is the method that isn't working properly

private async Task<T> HttpGetAsync<T>(string url, string token)
        {
            T result = default(T);

            try
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                HttpResponseMessage response = httpClient.GetAsync(url).Result;
                HttpContent content = response.Content;

                if (response.IsSuccessStatusCode)
                {
                    var jsonResponse = await content.ReadAsStringAsync();
                    result = JsonConvert.DeserializeObject<T>(jsonResponse);
                }
                else
                {
                    throw new Exception(((int)response.StatusCode).ToString() + " - " + response.ReasonPhrase);
                }
            }
            catch (Exception ex)
            {
                OnError(ex.ToString());
            }

            return result;
        }

So it will return the response as I said right

var jsonResponse = await content.ReadAsStringAsync();

But then it will return null in this line

result = JsonConvert.DeserializeObject<T>(jsonResponse);

And if I try to return a list (JsonConvert.DeserializeObject<List>) it will return this json error

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type...

On my code the T would be my Category entity

public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

This is my json

{
    "data": [
        {
            "id": 1,
            "name": "Alianzas"
        },
        {
            "id": 2,
            "name": "Pendientes"
        },
        {
            "id": 3,
            "name": "Pulseras"
        },
        {
            "id": 4,
            "name": "Colgantes"
        },
        {
            "id": 5,
            "name": "Gargantillas"
        },
        {
            "id": 6,
            "name": "Relojes"
        }
    ],
    "meta": {
        "totalCount": 6,
        "pageSize": 20,
        "currentPage": 1,
        "totalPages": 1,
        "hasNextPage": false,
        "hasPreviousPage": false
    }
}

I'm gessing the error here is trying to deserialize the json with the "data" and "meta" label ? Please help.

Upvotes: 1

Views: 727

Answers (1)

Kvble
Kvble

Reputation: 294

In this case you gotta have an object that should be something like

public class MyObject
{
    public List<Category> data { get; set; }
    public Meta meta { get; set; }
}

So you should also have a Meta class which describes all meta properties and then use it like this

HttpGetAsync<MyObject>(url, token);

Upvotes: 2

Related Questions