转换数组PHP数组到C# [英] Converting PHP array of arrays to C#

查看:178
本文介绍了转换数组PHP数组到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个C#客户端进行第三方API(我使用的是RestSharp的NuGet包)。他们的文档包含PHP的例子,我必须转换为C#。他们的大多数样本是足够简单,我已经能够将它们转换为C#,但我与他们中的一个挣扎,因为它接受一个数组的数组。下面是从他们的文档样本:

  $ PARAMS =阵列(
'user_key'=>'X',
'的client_id'=>'X,
'标签'=>阵列(
阵列(
'语言'=>'EN_US',
'名称'=>'英文标签',

阵列(
'语言'=>'fr_CA',
'名'=>法国标签 ,


);
$ CH = curl_init();
curl_setopt($ CH,CURLOPT_URL,'https://api.3rdparty.com/xxx/yyy');
curl_setopt($ CH,CURLOPT_POST,真正的);
curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列('apikey:YOURAPIKEY'));
curl_setopt($ CH,CURLOPT_POSTFIELDS,http_build_query($ params)方法);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);

$结果= curl_exec($ CH);

下面是我到目前为止有:

  VAR标签=新词典<字符串,字符串>()
{
{EN_US,英文标识},
{fr_CA ,法国标签}
};

变种要求=新RestRequest(/ XXX / YYY,Method.POST){RequestFormat = DataFormat.Json};
request.AddHeader(apikey,myApiKey);
request.AddParameter(user_key,myUserKey);
request.AddParameter(CLIENT_ID,myClientId);
request.AddParameter(标签?);

VAR的客户=新RestSharp.RestClient(https://api.3rdparty.com)
{
超时=超时,
的UserAgent =我的。 NET REST客户端
};
VAR响应= client.Execute(请求);



是否有人知道如何转换的标签字典成的格式,就相当于PHP的http_build_query ?


解决方案

http_build_query($ params)方法产生输出看起来像这样




  user_key = X&安培; CLIENT_ID = X&放大器;标号%5B0%5D%5Blanguage%5D = EN_US&放大器;标号%5B0%5D%5Bname%5D =英语+标签和放大器;标号%5B1%5D%5Blanguage%5D = fr_CA&放大器;标号%5B1%5D%5Bname%5D =法语+标签




其中,解码,是这样的:




  user_key = X&安培; CLIENT_ID = X&放大器;标签[0] [语言] = EN_US&放大器;标签[0] [名] =英文标签和放大器;标号[1] [语言] = fr_CA&放大器;标号[1] [名] =法国标签




所以,你应该能够做到:

  VAR要求=新RestRequest(/ XXX / YYY,方法。 POST); 
request.AddHeader(apikey,myApiKey);
request.AddParameter(user_key,myUserKey);
request.AddParameter(CLIENT_ID,myClientId);
的foreach(在labels.Select VAR项目((对我)=>新建{指数=我,语言= pair.Key,名称= pair.Value}))
{
request.AddParameter(的String.Format(标签[{0}] [语言],item.index),item.language);
request.AddParameter(的String.Format(标签[{0}] [名],item.index),item.name);
}

请注意,从试验,我注意到,RestSharp被编码空间 20%,而不是 + 。但愿不是问题


I am writing a C# client for a 3rd party API (I'm using the RestSharp nuget package). Their documentation contains PHP examples which I must translate to C#. Most of their samples are simple enough and I have been able to convert them to C# but I am struggling with one of them because it accepts an array of arrays. Here's the sample from their documentation:

$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);

Here's what I have so far:

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);

Does anybody know how to convert the "labels" dictionary into a format that would be equivalent to PHP's http_build_query?

解决方案

http_build_query($params) produces output that looks like this:

user_key=X&client_id=X&label%5B0%5D%5Blanguage%5D=en_US&label%5B0%5D%5Bname%5D=English+label&label%5B1%5D%5Blanguage%5D=fr_CA&label%5B1%5D%5Bname%5D=French+label

Which, decoded, looks like:

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

So, you should be able to do:

        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);
        }

Note that, from experimentation, I have noticed that RestSharp is encoding a space as %20 rather than as +. Hopefully not a problem.

这篇关于转换数组PHP数组到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆