将Perl Hash转换为c#keyvaluepair

时间:2014-09-11 08:41:53

标签: c# perl

我有一个调用c#exe的perl脚本。我需要传递像这样的数据结构

 testA => 123, testB => 111 etc 

我需要在perl中构造这个变量,并将它作为命令行参数传递给我的c#exe。

我选择将此值存储为perl中的hash和c#中的keyvaluepair。

所以,在perl代码中,我正在构建

 %test_id

我无法将其传递给该计划。我尝试了一些东西,但似乎双方都需要进行一些解析。就像将hash hash转换为string;然后在c#侧将字符串转换为kvp。这是预期的还是有更好的方法来做到这一点。

这是我尝试过的一种方法。

 for(keys %ids)
{
     my $key = join (":", $_, $ids{$_});
     $name = join (";", $key, $name);
}

我用

调用了c#exe
 UpdateCode.exe -add $name;

就像

 UpdateCode.exe -add "testA:123;testB:111"

所以,当c#代码看到这个时。我必须做

   string[] splits = args[1].split(new char[] {';'});

  foreach(string split in splits)
  {
     //You get the idea 
     //split again and construct KVP
   }

似乎很多代码。有没有更好的办法?我在perl模块中尝试过Dumper,但即便如此,我还是需要在c#侧进行解析。

感谢您寻找

1 个答案:

答案 0 :(得分:2)

使用JSON

C#中有等效的库来解码序列化数据。

use strict;
use warnings;

use JSON;

my %test_id = ( testA => 123, testB => 111 );

print encode_json(\%test_id);

输出:

{"testA":123,"testB":111}
相关问题