I have this problem, I believe it’s something in Json, but I’ve tried everything and I’m not able to return the objects.
My code:
public List<Product> List()
{
var retorno = new List<Product>();
try
{
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://teste.teste1.com.br/app/Api/V1/Products/list"))
{
client.DefaultRequestHeaders.Add("x-user-email", "[email protected]");
client.DefaultRequestHeaders.Add("x-api-key", "eyJ0edsasa3121dasdas312dsa");
client.DefaultRequestHeaders.Add("x-store-key", "1");
request.Content = new StringContent("Content-Type", Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("accept", "application/json;charset=UTF-8");
var response = client.SendAsync(request);
//var response = client.GetStringAsync(uprApi + "app/Api/V1/Products/list");
response.Wait();
retorno = JsonConvert.DeserializeObject<Product[]>(response.Result.ToString()).ToList();
}
}
}
catch (Exception)
{
throw;
}
return retorno;
}
Image Error:
EDIT:
My json:
{
"success": true,
"offset": 0,
"result": {
"error": false,
"registers_count": 5,
"pages_count": 1,
"page": 1,
"data": [
{
"product": {
"sku": "01",
"name": "Produto teste 01",
"description": "Produto de teste 01.",
"status": "enabled",
"qty": 30,
"price": 149.9,
"list_price": 199.9,
"weight_gross": 0.1,
"weight_liquid": 0.1,
"height": 1,
"width": 1,
"length": 1,
"items_per_package": "1",
"brand": "Colcci",
"ean": null,
"ncm": null,
"categories": [
{
"code": "339",
"name": "ESPORTES/Academia e Fitness/Roupas"
}
]
}
},
{
"product": {
"sku": "3",
"name": "Produto DESATIVADO",
"description": "xyz",
"status": "disabled",
"qty": 0,
"price": 5.8,
"list_price": null,
"weight_gross": 0.1,
"weight_liquid": 0.1,
"height": 1,
"width": 1,
"length": 1,
"items_per_package": "1",
"brand": "Colcci",
"ean": null,
"ncm": null,
"categories": [
{
"code": null,
"name": null
}
],
"variation_attributes": [
"UN"
]
}
},
{
"product": {
"sku": "2",
"name": "Produto X",
"description": "xyz",
"status": "enabled",
"qty": 0,
"price": 5.8,
"list_price": null,
"weight_gross": 0.1,
"weight_liquid": 0.1,
"height": 1,
"width": 1,
"length": 1,
"items_per_package": "1",
"brand": "Colcci",
"ean": null,
"ncm": null,
"categories": [
{
"code": null,
"name": null
}
],
"variation_attributes": [
"UN"
]
}
},
{
"product": {
"sku": "1",
"name": "Produto",
"description": "Descrição do Produto Criado",
"status": "disabled",
"qty": 176,
"price": 5.9,
"list_price": 5.8,
"weight_gross": 0.1,
"weight_liquid": 0.1,
"height": 1,
"width": 1,
"length": 1,
"items_per_package": "1",
"brand": "Colcci",
"ean": "7896044963486",
"ncm": null,
"categories": [
{
"code": "3",
"name": "FEMININO/Roupas/Blusas e Camisas"
}
],
"variations": [
{
"sku": "P_B",
"qty": 12,
"EAN": null,
"price": 5.8,
"list_price": 5.8,
"images": [],
"variant": [
{
"size": "p"
},
{
"color": "Branco"
}
]
},
{
"sku": "M_P",
"qty": 14,
"EAN": null,
"price": 5.8,
"list_price": 5.8,
"images": [],
"variant": [
{
"size": "m"
},
{
"color": "Preto"
}
]
},
{
"sku": "ABC1",
"qty": 150,
"EAN": null,
"price": 5.9,
"list_price": 5.8,
"images": [],
"variant": [
{
"size": "p"
},
{
"color": "Azul"
}
]
}
]
}
},
{
"product": {
"sku": "GG004",
"name": "Gabinete Gamer",
"description": "Gabinete Gamer com RGB",
"status": "enabled",
"qty": 6,
"price": 399.9,
"list_price": 399.9,
"weight_gross": 0.3,
"weight_liquid": 0.25,
"height": 9,
"width": 3,
"length": 5,
"items_per_package": "1",
"brand": "Colcci",
"ean": null,
"ncm": null,
"categories": [
{
"code": "276",
"name": "CELULARES & ELETRÔNICOS/Informática/Games"
}
],
"images": [
"https://manairadigitalteste.conectala.com.br/app/assets/images/product_image/4B3451F8-C862-7884-D169-5F256FA31F91/16581443192118.jpg"
],
"published_marketplace": {
"ManairaDigital": "5_ManairaDigital"
},
"marketplace_offer_links": [
{
"name": "ManairaDigital",
"href": "https://manairadigital.com.br/gabinete-gamer-29/p"
}
]
}
}
]
}
}
EDIT 2:
HEADERS:
{"x-user-email", "[email protected]"},
{"x-api-key", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb2Rfc3RvcmUiOjMsImNvZF9jb21wYW55Ijo3fQ.E_sxhnwcmN5GtNGRYbVD66ciMi3JoJNjormS1q3mxYg"},
{"x-store-key", "3"},
{"Content-Type", "application/json"},
{"accept", "application/json;charset=UTF-8"}
teste teste teste
teste teste teste
teste teste teste
teste teste teste
teste teste teste
teste teste teste
teste teste teste
teste teste teste
teste teste teste
teste teste teste
teste teste teste
EDIT 3
enter image description here
2
Answers
First of all using
Wait
you have a synchronous code and will block the thread until the task completes. You should await the request so it will asynchronous wait the task to complete. Try this instead and mark async the method that wraps this request to get the json response asynchronously:Then paste json as classes from menu in visual studio
Edit->Paste Special->Paste JSON as classes
. If you do this will get these classes:Last in your controller do this:
A note about HttpClient:
For a better design read this: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0
Yes, You are right. I had same problem when deserialize the class. The problem is on you json when deserialize the object.Use this class when deserialize.