1

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.

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

T is just a generic he gets from calling the function. HttpGetAsync<T>. but yes I agree with the rest.
actually you're right I'm going to edit the answer
public List<Category> categories { get; set; } Should be public List<Category> data { get; set; }, or he should change its json :)
Thank you so much! I did a BaseEntity object that would accept a generic entity with a generic list and Meta as properties, then I just specified the entity Category on my list. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.