简体   繁体   English

从Web api获取json数组到c#

[英]Get json array from Web api into c#

I´m having a SQL database and I want to get the SQL values from a API into c# code. 我有一个SQL数据库,我想将APISQL值转换为c#代码。 The API contains multiple rooms with room attributes, and each room have a Guid as a id. 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 (eg [1,2,3]) into type 'Class2' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly. Newtonsoft.Json.JsonSerializationException:'无法将当前JSON数组(例如[1,2,3])反序列化为类型'Class2',因为该类型需要JSON对象(例如{“name”:“value”})才能正确反序列化。 To fix this error either change the JSON to a JSON object (eg {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (eg ICollection, IList) like List that can be deserialized from a JSON array. 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如List从JSON数组反序列化。 JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。 Path '', line 1, position 1.' 路径'',第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
  }
]

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: 编辑1:

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

   // and so on
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM