![](/img/trans.png)
[英]Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: S. Path '', line 0, position 0. Only on certain build
[英]Error Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: S. Path '', line 0, position 0.'
我有這個問題,我相信這是 Json 中的問題,但我已經嘗試了一切,但我無法返回對象。
我的代碼:
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", "victor@teste.com.br");
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;
}
圖像錯誤:
編輯:
我的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"
}
]
}
}
]
}
}
編輯2:標題:
{"x-user-email", "victor@manairadigital.com.br"},
{"x-api-key", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb2Rfc3RvcmUiOjMsImNvZF9jb21wYW55Ijo3fQ.E_sxhnwcmN5GtNGRYbVD66ciMi3JoJNjormS1q3mxYg"},
{"x-store-key", "3"},
{"Content-Type", "application/json"},
{"accept", "application/json;charset=UTF-8"}
測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測試測
編輯 3在此處輸入圖像描述
首先使用Wait
你有一個同步代碼並且會阻塞線程直到任務完成。 您應該等待請求,以便異步等待任務完成。 試試這個,並將包裝這個請求的方法標記為異步:
public async Task GetAsync()
{
var httpRequestMessage = new HttpRequestMessage(
HttpMethod.Get,
"https://example.com/api/endpoint" // here goes your url
{
Headers =
{
{"key", "value"}, // here goes your headers
}
}
var client = new HttpClient();
using var response = await client.SendAsync(httpRequestMessage);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
var root = JsonSerializer.Deserialize<Rootobject>(responseBody);
}
異步獲取 json 響應,然后從 Visual Studio Edit->Paste Special->Paste JSON as classes
的菜單中將 json 粘貼為類。 如果你這樣做會得到這些類:
public class Rootobject
{
public bool success { get; set; }
public int offset { get; set; }
public Result result { get; set; }
}
public class Result
{
public bool error { get; set; }
public int registers_count { get; set; }
public int pages_count { get; set; }
public int page { get; set; }
public Datum[] data { get; set; }
}
public class Datum
{
public Product product { get; set; }
}
public class Product
{
public string sku { get; set; }
public string name { get; set; }
public string description { get; set; }
public string status { get; set; }
public int qty { get; set; }
public float price { get; set; }
public float? list_price { get; set; }
public float weight_gross { get; set; }
public float weight_liquid { get; set; }
public int height { get; set; }
public int width { get; set; }
public int length { get; set; }
public string items_per_package { get; set; }
public string brand { get; set; }
public string ean { get; set; }
public object ncm { get; set; }
public Category[] categories { get; set; }
public string[] variation_attributes { get; set; }
public Variation[] variations { get; set; }
public string[] images { get; set; }
public Published_Marketplace published_marketplace { get; set; }
public Marketplace_Offer_Links[] marketplace_offer_links { get; set; }
}
public class Published_Marketplace
{
public string ManairaDigital { get; set; }
}
public class Category
{
public string code { get; set; }
public string name { get; set; }
}
public class Variation
{
public string sku { get; set; }
public int qty { get; set; }
public object EAN { get; set; }
public float price { get; set; }
public float list_price { get; set; }
public object[] images { get; set; }
public Variant[] variant { get; set; }
}
public class Variant
{
public string size { get; set; }
public string color { get; set; }
}
public class Marketplace_Offer_Links
{
public string name { get; set; }
public string href { get; set; }
}
你是對的。 反序列化課程時我遇到了同樣的問題。 反序列化對象時,問題出在您的 json 上。反序列化時使用此類。
public class Rootobject
{
public bool success { get; set; }
public int offset { get; set; }
public Result result { get; set; }
}
public class Result
{
public bool error { get; set; }
public int registers_count { get; set; }
public int pages_count { get; set; }
public int page { get; set; }
public Datum[] data { get; set; }
}
public class Datum
{
public Product product { get; set; }
}
public class Product
{
public string sku { get; set; }
public string name { get; set; }
public string description { get; set; }
public string status { get; set; }
public int qty { get; set; }
public float price { get; set; }
public float? list_price { get; set; }
public float weight_gross { get; set; }
public float weight_liquid { get; set; }
public int height { get; set; }
public int width { get; set; }
public int length { get; set; }
public string items_per_package { get; set; }
public string brand { get; set; }
public string ean { get; set; }
public object ncm { get; set; }
public Category[] categories { get; set; }
public string[] variation_attributes { get; set; }
public Variation[] variations { get; set; }
public string[] images { get; set; }
public Published_Marketplace published_marketplace { get; set; }
public Marketplace_Offer_Links[] marketplace_offer_links { get; set; }
}
public class Published_Marketplace
{
public string ManairaDigital { get; set; }
}
public class Category
{
public string code { get; set; }
public string name { get; set; }
}
public class Variation
{
public string sku { get; set; }
public int qty { get; set; }
public object EAN { get; set; }
public float price { get; set; }
public float list_price { get; set; }
public object[] images { get; set; }
public Variant[] variant { get; set; }
}
public class Variant
{
public string size { get; set; }
public string color { get; set; }
}
public class Marketplace_Offer_Links
{
public string name { get; set; }
public string href { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.