0

I am currently working on an app and want to get some data of tickets. For this I use HttpClient and the get method to connect to an api. By opening the page i get the following exception:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[App.Models.Ticket]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'id', line 1, position 6.'

Do you know a solution? And yes I have an URL inserted.

Ticket.cs

public class Ticket
    {
        public string id { get; set; }
        public string message { get; set; }
        public List<string> notes { get; set; }
        public string status { get; set; }
        public string subject { get; set; }
    }

LoadTickets

public async void LoadTickets()
        {
            var content = "";
            HttpClient client = new HttpClient();
            var RestURL = "https://...";
            client.BaseAddress = new Uri(RestURL);
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await client.GetAsync(RestURL);
            content = await response.Content.ReadAsStringAsync();
            var Items = JsonConvert.DeserializeObject<List<Ticket>>(content);
            ListViewTicket.ItemsSource = Items;
        }

Api

{
  "id": "1",
  "status": "closed",
  "subject": "Gerätefehler -21 bei SAP App",
  "message": "Beim Starten der SAP App kommt immer ein Fehler -21. Was muss ich machen?",
  "notes": [
    "Vielen Dank für Ihre Anfrage. Es wurde das Ticket 1 für Sie eröffnet",
    "Das ist ein eher kurzer Kommentar auf das Ticket.",
    "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet",
    "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
  ]
}
connection: keep-alive 
 content-type: application/json 
 date: Mon03 Jan 2022 15:08:36 GMT 
 keep-alive: timeout=20 
 transfer-encoding: chunked 
Serge
  • 40,935
  • 4
  • 18
  • 45
Pentium
  • 5
  • 3

1 Answers1

0

you have only one item in your api, so try this

Ticket item = JsonConvert.DeserializeObject<Ticket>(content);

or you can convert it to list

List<Ticket> items= new List<Ticket>{item};
 ListViewTicket.ItemsSource = Items;

or change your Api to return a list instead of one item

[
{
  "id": "1",
  "status": "closed",
  "subject": "Gerätefehler -21 bei SAP App",
  "message": "Beim Starten der SAP App kommt immer ein Fehler -21. Was muss ich machen?",
  "notes": [
    "Vielen Dank für Ihre Anfrage. Es wurde das Ticket 1 für Sie eröffnet",
    "Das ist ein eher kurzer Kommentar auf das Ticket.",
    "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet",
    "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
  ]
}
]

if you mean notes than you can get them

List<string> notes = JsonConvert.DeserializeObject<Ticket>(content).notes;
Serge
  • 40,935
  • 4
  • 18
  • 45