Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PHP数组转换为C#_C#_Php_Json.net_Restsharp - Fatal编程技术网

将PHP数组转换为C#

将PHP数组转换为C#,c#,php,json.net,restsharp,C#,Php,Json.net,Restsharp,我正在为第三方API编写一个C#客户端(我正在使用RestSharp nuget包)。他们的文档包含PHP示例,我必须将其翻译成C。他们的大多数示例都非常简单,我已经能够将它们转换为C#,但我正在使用其中一个示例,因为它接受一个数组。以下是他们文档中的示例: $params = array ( 'user_key' => 'X', 'client_id'=> 'X, 'label' => array( array(

我正在为第三方API编写一个C#客户端(我正在使用RestSharp nuget包)。他们的文档包含PHP示例,我必须将其翻译成C。他们的大多数示例都非常简单,我已经能够将它们转换为C#,但我正在使用其中一个示例,因为它接受一个数组。以下是他们文档中的示例:

$params = array (
    'user_key' => 'X',
    'client_id'=> 'X,
    'label' => array(
        array(
            'language' => 'en_US',
            'name' => 'English label',
        ),
        array(
            'language' => 'fr_CA',
            'name' => 'French label',
        ),
    ),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.3rdparty.com/xxx/yyy'); 
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apikey: YOURAPIKEY'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
以下是我目前掌握的情况:

var labels = new Dictionary<string, string>() 
{
    { "en_US", "English Label" },
    { "fr_CA", "French Label" }
};

var request = new RestRequest("/xxx/yyy", Method.POST) { RequestFormat = DataFormat.Json };
request.AddHeader("apikey", myApiKey);
request.AddParameter("user_key", myUserKey);
request.AddParameter("client_id", myClientId);
request.AddParameter("label", ???);

var client = new RestSharp.RestClient("https://api.3rdparty.com")
{
    Timeout = timeout,
    UserAgent = "My .NET REST Client"
};
var response = client.Execute(request);
var标签=新字典()
{
{“en_US”,“英文标签”},
{“fr_CA”,“法语标签”}
};
var request=new RestRequest(“/xxx/yyy”,Method.POST){RequestFormat=DataFormat.Json};
AddHeader(“apikey”,myApiKey);
AddParameter(“用户密钥”,myUserKey);
AddParameter(“客户端id”,myClientId);
请求.添加参数(“标签”、?);
var client=new RestSharp.RestClient(“https://api.3rdparty.com")
{
超时=超时,
UserAgent=“我的.NET REST客户端”
};
var response=client.Execute(请求);
有人知道如何将“标签”字典转换成与PHP的http构建查询等价的格式吗?

您可以使用这是该PHP函数的镜像实现。

http构建查询($params)
生成如下输出:

解码后,看起来像:

因此,您应该能够做到:

        var request = new RestRequest("/xxx/yyy", Method.POST);
        request.AddHeader("apikey", myApiKey);
        request.AddParameter("user_key", myUserKey);
        request.AddParameter("client_id", myClientId);
        foreach (var item in labels.Select((pair, i) => new { index = i, language = pair.Key, name = pair.Value }))
        {
            request.AddParameter(string.Format("label[{0}][language]", item.index), item.language);
            request.AddParameter(string.Format("label[{0}][name]", item.index), item.name);
        }

注意,从实验中,我注意到RestSharp将空间编码为
%20
,而不是
+
。希望如此。

太好了!谢谢。顺便说一句,我真的很喜欢你如何重新设计列表以包含每个项目的索引。在每个循环中声明一个整数并将其递增一更方便。这对我来说不是很好。。。它不能正确处理嵌套字典。
user_key=X&client_id=X&label[0][language]=en_US&label[0][name]=English label&label[1][language]=fr_CA&label[1][name]=French label
        var request = new RestRequest("/xxx/yyy", Method.POST);
        request.AddHeader("apikey", myApiKey);
        request.AddParameter("user_key", myUserKey);
        request.AddParameter("client_id", myClientId);
        foreach (var item in labels.Select((pair, i) => new { index = i, language = pair.Key, name = pair.Value }))
        {
            request.AddParameter(string.Format("label[{0}][language]", item.index), item.language);
            request.AddParameter(string.Format("label[{0}][name]", item.index), item.name);
        }