0

I am getting the above error while trying to get the Json format from a url.

  public class Product
{
    public int Id { get; set; }
    [JsonProperty("externalId")]
    public int ExternalId { get; set; }
    [JsonProperty("code")]
    public string Code { get; set; }
    [JsonProperty("description")]
    public string Description { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("barcode")]
    public int Barcode { get; set; }
    [JsonProperty("retailPrice")]
    public double RetailPrice { get; set; }
    [JsonProperty("wholesalePrice")]
    public double WholesalePrice { get; set; }
    [JsonProperty("discount")]
    public double Discount { get; set; }
}

public class Products
{
    public List<Product> GetProducts { get; set; }
}

and in my controller I have:

        public async Task<ActionResult> Index()
    {
        string url = "https://cloudonapi.oncloud.gr/s1services/JS/updateItems/cloudOnTest";
        HttpClient client = new HttpClient();

        var httpResponseMessage = await client.GetAsync(url);
        httpResponseMessage.EnsureSuccessStatusCode();
        string jsonResponse = await httpResponseMessage.Content.ReadAsStringAsync();

        var list = JsonConvert.DeserializeObject<Products>(jsonResponse);

        return View(list);

    }

I keep getting the Unexpected character encountered while parsing value: . Path '', line 0, position 0. error

4
  • 3
    Have you inspected the jsonResponse string to see what it is? Commented Apr 14, 2022 at 7:53
  • @JamesGould Yes it's a string, formatted wrong, like: "\u001f‹\b\0\0\0\0\0\0\0µ•έnΪ0\u0014Η_ε(WT¥Αv0\u001f½\vΑ΄Ρ\f±’\u008c±M»Θ \u0090\u0018L@5¤―Τwh¥ξ««iκϋμ8”¬$™ΤNτ*Ρ±}\u008e\u007fηγοScq2\u001cΖ‹…qΈ\u009c\u009fΔe0FΡ22\u000eί\u009e\u001aρj\u0019Ο§ΡΔ\u001d\u0019p\b†Ε\u001aΌV5ΚΖp6\u00\0" etc.. Commented Apr 14, 2022 at 7:56
  • well there you have it, you need to request the data with the right encoding, and you need to adjust your Products Object to match the json structure Commented Apr 14, 2022 at 7:58
  • I think you also need to change the Products to public class Products { public bool success { get; set; } public List<Product> data { get; set; } } Commented Apr 14, 2022 at 8:35

1 Answer 1

2

So. \u001f means you received the byte 1F. is in the range of extended ASCII with encoding 8B. \b is the ASCII backspace character with encoding 08.

1F 8B is the magic number for GZIP (and the 08 is the compression method: DEFLATE).

So, the server's one of the broken ones which gives you back a gzip-compressed response, even though you didn't say you could accept gzip-compressed responses.

You can tell HttpClient to automatically decompress gzip-compressed responses:

HttpClientHandler handler = new HttpClientHandler()
{
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
using var client = new HttpClient(handler));

var httpResponseMessage = await client.GetAsync(url);
// ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.