0

I am trying to consume API receiving JSON objects. The problem is my API sends these objects nested in square brackets, which makes me deseralize it as a List instead of a single object.

I am declaring this variable as an array of this instance of the class.

SomeModel[] variable = new SomeModel[1];

using (var httpClient = new HttpClient())
                {
                    using (var response = await httpClient.GetAsync("https://someurl.dev.local/getInfo/" + id))
                    {
                        if (response.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            string apiResponse = await response.Content.ReadAsStringAsync();
                            variable = JsonConvert.DeserializeObject<SomeModel[]>(apiResponse);
                                                       
                        }
                        else
                            ViewBag.StatusCode = response.StatusCode;
                    }
                }

Thiis is a sample of a JSON object I should be receiving, which I tested using Postman:

[
 {
    "pri_key": "7005210446", //concatenation of this_nbr & that_nbr
    "dl_load_date": "2021-11-25T00:00:00Z",
    "this_nbr": 7005210,
    "that_nbr": 446,
    "Passtest": "Eligible"
 }
]

...And this is SomeModel class:

    namespace ThisSolution.Models
{
    public partial class SomeModel
    {
        public DateTime? DlLoadDate { get; set; }
        public int? ThisNbr{ get; set; }
        public int? ThatNbr { get; set; }
        public string Passtest { get; set; }
        public string PriKey { get; set; }
    }
}

I am getting is this error:

JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

How can I deserialize or is something wrong in my code?

6
  • You should post json and somemodel class. Commented Nov 26, 2021 at 18:05
  • apiResponse might contain html and not json. Commented Nov 26, 2021 at 18:07
  • Paste the response Commented Nov 26, 2021 at 18:21
  • @Serge I just edited the question and added json and SomeModel class. Thank you Commented Nov 26, 2021 at 18:24
  • @ScottyD0nt I've been testing this API with Postman and it is sending json objects, not html. Commented Nov 26, 2021 at 18:25

1 Answer 1

2

You need to fix your model

public partial class SomeModel
{
    [JsonProperty("pri_key")]
    public string PriKey { get; set; }

    [JsonProperty("dl_load_date")]
    public DateTimeOffset DlLoadDate { get; set; }

    [JsonProperty("this_nbr")]
    public int? ThisNbr { get; set; }

    [JsonProperty("that_nbr")]
    public int? ThatNbr { get; set; }

    [JsonProperty("Passtest")]
    public string Passtest { get; set; }
}
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.