【问题标题】:How do I get a list of webapi objects?如何获取 Web api 对象列表?
【发布时间】:2019-06-14 13:48:26
【问题描述】:

我是 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,但我还没有弄清楚如何。

【问题讨论】:

    标签: c# json xamarin


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 2019-04-07
      • 1970-01-01
      相关资源
      最近更新 更多