6

I´m having a SQL database and I want to get the SQL values from a API into c# code. The API contains multiple rooms with room attributes, and each room have a Guid as a id.

This code works but I only get one room out of this:

string url = "https://api.booking.com/api/room/35bf3c4d-9b5b-40fd-bcf4-a4c2c6c564bc";

HttpClient client = new HttpClient();

string response = await client.GetStringAsync(url);

var data = JsonConvert.DeserializeObject<Class2>(response);

When i remove the id (Guid) to get all rooms i get this error when i launch the application:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Class2' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'

This is the code without the Guid:

string url = "https://api.booking.com/api/room";

HttpClient client = new HttpClient();

string response = await client.GetStringAsync(url);

var data = JsonConvert.DeserializeObject<Class2>(response);

My question is in short terms "How can i get a json array from a api?".

Thanks in advance!

Edit:

This is the code for class2:

public class Class2
{
    public string name { get; set; }
    public string id { get; set; }
    public int seats { get; set; }
    public int? availableFrom { get; set; }
    public int? availableTo { get; set; }
}

And this is a longer output from a room in the API:

[
{
    "name": "Rum 1",
    "id": "a31d1fc8-df29-419c-8308-f8bc884b378e",
    "seats": 10,
    "availableFrom": null,
    "availableTo": null
  },
  {
    "name": "Rum 2",
    "id": "7defd34d-222d-4980-b28f-e616e8b9003c",
    "seats": 5,
    "availableFrom": null,
    "availableTo": null
  },
  {
    "name": "Skrubben",
    "id": "b20390ff-703b-4d80-8c2f-32c0f27158bc",
    "seats": 5,
    "availableFrom": 10,
    "availableTo": 11
  },
  {
    "name": "Hangaren",
    "id": "b89cbacd-c738-477f-aff2-7f22c2b2cd5c",
    "seats": 100,
    "availableFrom": null,
    "availableTo": null
  },
  {
    "name": "Tv-rummet",
    "id": "ea6a290f-209b-4ccb-91a4-65d82a674bad",
    "seats": 10,
    "availableFrom": null,
    "availableTo": null
  },
  {
    "name": "Projektor-rummet",
    "id": "3136a56a-4a28-4939-aca8-806534c808f7",
    "seats": 12,
    "availableFrom": null,
    "availableTo": null
  },
  {
    "name": "Skolsalen",
    "id": "05f73582-3734-453f-aeb3-36daf8884912",
    "seats": 30,
    "availableFrom": null,
    "availableTo": null
  }
]
8
  • Try JsonConvert.DeserializeObject<Class2[]>(response); Commented May 25, 2018 at 9:36
  • When I do that i cant reach all the attributes inside the rooms (for example Name) @DmitryEgorov Commented May 25, 2018 at 9:41
  • Perhaps, objects returned in the array by .../api/room are not as detailed as a single object instance returned by .../api/room/{guid}. Could you please provide a sample output of the two API requests and Class2 definition as well? Commented May 25, 2018 at 9:45
  • Check my edit @DmitryEgorov Commented May 25, 2018 at 9:58
  • Seems to be output from .../api/room/{guid}. What's the output from .../api/room? Commented May 25, 2018 at 10:00

2 Answers 2

5

As the error suggests,

You are deserializing an array into a class. It will never work. You need to deserialize it into either an array or a list using the below code snippet.

Try:
var data = JsonConvert.DeserializeObject<List<Class2>>(response);
or
var data = JsonConvert.DeserializeObject<Class2[]>(response);
and it will work fine.

EDIT 1:

foreach(var room in data)
{
   string id = room.id;
   string name = room.name;

   // and so on
}
Sign up to request clarification or add additional context in comments.

6 Comments

Okey, i tried the first example but when i do that i can't reach the attributes in the rooms.
With first example, you get a list of rooms. Now try to iterate it using a foreach loop and find the attributes.
Okey, i will come back with the results :)
Sure, post the code snippet if possible for more clarity.
Haha, sorry but i don't really have the knowledge to write the foreach loop :(
|
0

multiple Class2 objects must be use List or Array type to convert them, you can try JsonConvert.DeserializeObject<List<Class2>>(response)

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.