如何获取Webapi对象列表?

时间:2019-01-21 07:48:09

标签: c# json xamarin

我是webapis的新手,希望我在这里提出正确的问题。

我创建了一个webapi。此webapi具有以下字符串:

[
   {
      "titel":"Erste News",
      "date":"2019-01-18T23:00:00.000Z",
      "message":"Hallo dieses sind die ersten News from strapi",
      "Zahl":10,
      "_id":"5c41aecc21efaf11c423afcc",
      "createdAt":"2019-01-18T10:47:40.934Z",
      "updatedAt":"2019-01-18T10:47:40.966Z",
      "__v":0,
      "id":"5c41aecc21efaf11c423afcc",
      "Titelbild":null
   },
   {
      "titel":"News",
      "date":"2019-01-24T23:00:00.000Z",
      "message":"**Test**\n*test*\n__underlined text__",
      "Zahl":null,
      "_id":"5c41b481c10f08122377f92a",
      "createdAt":"2019-01-18T11:12:01.284Z",
      "updatedAt":"2019-01-18T11:12:01.294Z",
      "__v":0,
      "id":"5c41b481c10f08122377f92a",
      "Titelbild":null
   },
   {
      "titel":"News mit Bild",
      "date":"2019-01-17T23:00:00.000Z",
      "message":"News mit Bild\n\n\n![text](http://localhost:1337/uploads/233db8c756ce4a67b8ccf2a1ed2e94ec.png)",
      "Zahl":null,
      "_id":"5c41c847cf82c4127f8f6e2f",
      "createdAt":"2019-01-18T12:36:23.815Z",
      "updatedAt":"2019-01-18T12:36:23.820Z",
      "__v":0,
      "id":"5c41c847cf82c4127f8f6e2f",
      "Titelbild":{
         "_id":"5c41c847cf82c4127f8f6e30",
         "name":"Bildschirmfoto 2019-01-18 um 09.58.05.png",
         "sha256":"BTMlaz8MBwq6BCascc_s98QNOn0Ly0YeBpaarD4rZFs",
         "hash":"92802b2976f441f6b1573e2557240bd1",
         "ext":".png",
         "mime":"image/png",
         "size":"114.04",
         "url":"/uploads/92802b2976f441f6b1573e2557240bd1.png",
         "provider":"local",
         "related":[
            "5c41c847cf82c4127f8f6e2f"
         ],
         "createdAt":"2019-01-18T12:36:23.830Z",
         "updatedAt":"2019-01-18T12:36:23.834Z",
         "__v":0,
         "id":"5c41c847cf82c4127f8f6e30"
      }
   }
]

如何创建一个我可以使用的对象(或者由于存在多个对象-对象列表)?

这是我当前拥有的代码:

public partial class MainPage : ContentPage
{

    public MainPage()
    {

        InitializeComponent();

        var httpClient = new HttpClient();
        var content =  httpClient.GetStringAsync(url);
        var test = content.Result;
    }
}

public class Titelbild
{
    public string _id { get; set; }
    public string name { get; set; }
    public string sha256 { get; set; }
    public string hash { get; set; }
    public string ext { get; set; }
    public string mime { get; set; }
    public string size { get; set; }
    public string url { get; set; }
    public string provider { get; set; }
    public List<string> related { get; set; }
    public DateTime createdAt { get; set; }
    public DateTime updatedAt { get; set; }
    public int __v { get; set; }
    public string id { get; set; }
}

public class RootObject
{
    public string titel { get; set; }
    public DateTime date { get; set; }
    public string message { get; set; }
    public int? Zahl { get; set; }
    public string _id { get; set; }
    public DateTime createdAt { get; set; }
    public DateTime updatedAt { get; set; }
    public int __v { get; set; }
    public string id { get; set; }
    public Titelbild Titelbild { get; set; }
}

我假设我必须使用json,但我还没有弄清楚怎么做。

1 个答案:

答案 0 :(得分:3)

首先从nuget软件包管理器安装Newtonsoft.Json(版本12.0.1)库的最新版本,并尝试使用此代码

var test = content.Result;
if (!string.IsNullOrEmpty(test))
{
   var resultList = JsonConvert.DeserializeObject<List<RootObject>>(test);
}