json - RestSharp可以在POST请求中发送List <string>吗?

标签 json serialization restsharp

我正在尝试让RestSharp与我拥有的 Restful 服务一起工作。除了通过POST传递的对象包含一个列表(在这种情况下为string的列表)之外,其他所有内容似乎都正常运行。

我的对象:

public class TestObj
{
    public string Name{get;set;}
    public List<string> Children{get;set;}
}

当此消息发送到服务器时,Children属性将作为带有内容System.Collections.Generic.List`1[System.String]的字符串发送。

这就是我发送对象的方式:
var client = new RestClient();
var request = new RestRequest("http://localhost", Method.PUT);

var test = new TestObj {Name = "Fred", Children = new List<string> {"Arthur", "Betty"}};
request.AddObject(test);
client.Execute<TestObj>(request);

我是在做错什么,还是RestSharp中的错误? (如果有所作为,则我使用的是JSON,而不是XML。)

最佳答案

这取决于您要击中的服务器,但是如果您要击中ASP.NET Web API Controller (可能还有其他服务器端技术),则可以通过循环添加集合中的每个项目来使用它:

foreach (var child in test.Children) 
    request.AddParameter("children", x));

关于json - RestSharp可以在POST请求中发送List <string>吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12284931/

相关文章:

json - 这个简洁的 JSON 文档的 Go 结构是什么?

c# 反序列化 JSON web api 响应

c# - 将 C# 的复杂类型转换为 YAML

c# - 如何使用 RestSharp 捕获异常

c# - 如何在 Windows Phone 8 中为 RestClient 设置超时?

c# - 将代码从 RestSharp 转换为 HttpClient

html - JSON 作为 Flex 中的 HTML 数据 - 超链接翻转

.net - 反序列化时如何配置ServiceStack.Text JsonSerializer 属性名称

ruby-on-rails - 序列化对象在我的开发箱上运行良好,Heroku 给出了 "TypeError (can' t dump 匿名类 Class)”

javascript - 从 NodeJS 中的 http 请求获取 JSON