Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get json array from Web api into c#

Tags:

json

c#

uwp

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
  }
]
like image 596
luddep Avatar asked May 25 '18 09:05

luddep


People also ask

How extract JSON data from API?

The easiest way to get data from an API is with fetch , which includes the . json() method to parse JSON responses into a usable JavaScript object literal or array automagically.

How do I get raw JSON data from REST API?

To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON.


1 Answers

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
}
like image 199
Sahil Sharma Avatar answered Oct 09 '22 04:10

Sahil Sharma