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 的格式为 "property":"value"。
但是,在我的例子中,我只有一个字符串数组:
[
"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; }
}
我正在使用 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 的格式为 "property":"value"。
但是,在我的例子中,我只有一个字符串数组:
[
"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; }
}