簡體   English   中英

RestSharp:無法反序列化數組

[英]RestSharp: Unable to deserialize array

我正在使用RestSharp。 我有以下代碼:

public void MyMethod()
{
       var client = new RestClient("https://test_site/api");
       var request = new RestRequest("/endpoint", Method.GET);

       var response = client.Execute<List<MyMapClass>>(request);
}

我的問題是,在我看到的所有示例中,JSON的形式均為“屬性”:“值”。

但是,就我而言,我只有一個字符串數組:

[
  "Str1",
  "Str2",
  "Str3"
]

所以我知道當JSON格式為“ property”:“ value”時,如何反序列化對象,但是我的問題是:如何反序列化字符串數組?

請注意方括號而不是花括號。 大括號表示對象,方括號表示數組。

var response = client.Execute<List<string>>(request);

要么

var response = client.Execute<string[]>(request);

您還可以在對象中具有數組(請參見顏色)

{
    "name": "iPhone 7 Plus",
    "manufacturer": "Apple",
    "deviceType": "smartphone_tablet",
    "searchKey": "apple_iphone_7_plus",
    "colors": ["red", "blue"]
}

相應的模型如下所示:

public class MyMapClass 
{
    public string Name { get; set; }
    public string Manufacturer { get; set; }
    public string DeviceType { get; set; }
    public string SearchKey { get; set; }
    public List<string> Colors { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM