从Web api获取json数组到c#

时间:2022-09-15 12:36:36

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.

我有一个SQL数据库,我想将API中的SQL值转换为c#代码。 API包含多个具有房间属性的房间,每个房间都有一个Guid作为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:

当我删除id(Guid)以获取所有房间时,我在启动应用程序时出现此错误:

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.'

Newtonsoft.Json.JsonSerializationException:'无法将当前JSON数组(例如[1,2,3])反序列化为类型'Class2',因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如List从JSON数组反序列化。 JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。路径'',第1行,第1位。'

This is the code without the Guid:

这是没有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?".

我的问题是简短的说法“如何从api中获取json数组?”。

Thanks in advance!

提前致谢!

Edit:

This is the code for class2:

这是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:

这是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
  }
]

2 个解决方案

#1


4  

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.

尝试:var data = JsonConvert.DeserializeObject >(response);或var data = JsonConvert.DeserializeObject (response);它会工作正常。

EDIT 1:

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

   // and so on
}

#2


0  

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

多个Class2对象必须使用List或Array类型来转换它们,你可以尝试JsonConvert.DeserializeObject >(响应)

#1


4  

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.

尝试:var data = JsonConvert.DeserializeObject >(response);或var data = JsonConvert.DeserializeObject (response);它会工作正常。

EDIT 1:

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

   // and so on
}

#2


0  

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

多个Class2对象必须使用List或Array类型来转换它们,你可以尝试JsonConvert.DeserializeObject >(响应)