C# httpclient后嵌套字典
我有一个错误: 严重性代码说明项目文件行抑制状态错误 CS1503参数1:无法从转换 “C# httpclient后嵌套字典,c#,arrays,dictionary,httpclient,C#,Arrays,Dictionary,Httpclient,我有一个错误: 严重性代码说明项目文件行抑制状态错误 CS1503参数1:无法从转换 “System.Collections.Generic.Dictionary”到 “System.Collections.Generic.IEnumerable 当我使用HttpClient var arg_employee = new Dictionary<string, Dictionary<string, string>> { { "filter_data"
System.Collections.Generic.Dictionary
”到
“System.Collections.Generic.IEnumerable
当我使用HttpClient
var arg_employee = new Dictionary<string, Dictionary<string, string>>
{
{
"filter_data",
new Dictionary<string, string>
{
{"user_name", "admin"},
}
},
};
var content = new FormUrlEncodedContent(arg_employee);
// ... Use HttpClient.
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.PostAsync(uRL, content))
using (HttpContent responseContent = response.Content)
{
// ... Read the string.
return await responseContent.ReadAsStringAsync();
var arg\u employee=新字典
{
{
“过滤数据”,
新词典
{
{“用户名”,“管理员”},
}
},
};
var内容=新的FormUrlEncodedContent(arg_员工);
// ... 使用HttpClient。
使用(HttpClient=new HttpClient())
使用(httpresponsemessageresponse=wait client.PostAsync(uRL,内容))
使用(HttpContent-responseContent=response.Content)
{
//…读字符串。
return wait responseContent.ReadAsStringAsync();
如何发布嵌套字典或数组?
FormUrlEncodedContent
接受IEnumerable nameValueCollection
,而不是字典
。请参阅
例如,要传递字典,您应该将其序列化为json
第一步,使用Newtonsoft.json
获取字典的json表示(您可以手动序列化它或使用其他库)
恐怕不可能。您将以FormUrlEncodedContent的形式发送它们。您将能够以JSON或XML的形式发送此类复杂对象。
using Newtonsoft.Json
var arg_employee = new Dictionary<string, Dictionary<string, string>>
{
{
"filter_data",
new Dictionary<string, string>
{
{"user_name", "admin"},
}
},
};
var jsonDictionary = JsonConvert.SerializeObject(arg_employee );
var content = new StringContent(jsonDictionary , Encoding.UTF8, "application/json");