【问题标题】:How to convert laravel array to ruby hash access如何将 laravel 数组转换为 ruby​​ 哈希访问
【发布时间】:2021-11-15 18:56:31
【问题描述】:

我有一个数组来存储这样的一些数据,这是我正在使用的确切输入

{
    "accepted" => {
        "number_of_order" => {
            "condition" => "between_number", "value" => "0&5"
        }
    }, "removed" => {
        
    }
}

现在我正在尝试将此数组转换为像这样的 ruby​​ 哈希访问,这是我的预期输出,这不是我现在得到的

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    condition: between_number
    value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}

但我无法在 ruby​​ 哈希访问中转换数组。我也尝试将 YAML 转储 Yaml::dump($array); 转换为 YAML,然后进行 ruby​​ 哈希访问,但没有任何效果。

基本上,我正在尝试解决这个问题

How to get data from ruby hash access to laravel 8?

您可以参考上面的链接,我将这个 ruby​​ 哈希转换为 laravel 数组,但现在我也想将此数组转换为 ruby​​ 哈希。

我当前用于将数组转换为 ruby​​ 哈希的代码

$jsonEncoded = '{"accepted":{"number_of_order":{"consition":"between_number","value":"0&5"}}}';

$array = json_decode($jsonEncoded, true);

dump(Yaml::dump($array));

我当前用于将 ruby​​ 哈希转换为数组的代码

$filter_data = str_replace("!ruby/hash:ActiveSupport::HashWithIndifferentAccess", "", $filter_data);
$yamlContents = Yaml::parse($filter_data);

【问题讨论】:

  • 你的问题我不清楚。您写道,您有一个想要转换为 Ruby 的 Lavaral 数组,但随后您发布了一个已经是有效 Ruby 的嵌套散列。然后你写了关于加密的文章,但你的例子中没有加密值。您能否详细说明确切的输入数据和预期输出并分享您已有的代码?
  • 您好,@spickermann 很抱歉您不清楚共享数组是确切的输入,之后,我分享了 ruby​​ 代码,这实际上是我想要的预期输出。我正在编辑我的问题以分享我的代码。你可以参考我分享的那个链接,这样你就可以正确理解了。很抱歉使用加密关键字。
  • @HitenGal 你能告诉我你想要activerecord格式yaml输出的原因吗,我不明白我在想你无法加载yaml并想将它从ruby转换为php的问题反之亦然
  • @Chandan 我必须像这样存储数据。因为有一个 Shopify 应用程序在 ruby​​ 中使用 ActiveSupport 方法提供强大的数据,所以我必须使用 laravel 管理这些数据。

标签: ruby laravel


【解决方案1】:

要在 ruby​​ 中将 hash 转换为 yaml,我们可以使用 yaml library

require 'yaml'

data = {
    "accepted" => {
        "number_of_order" => {
            "condition" => "between_number", "value" => "0&5"
        }
    }, "removed" => {
        
    }
}

puts data.to_yaml

要在php中将关联数组转换为yaml,我们可以使用Symfony Yaml

use Symfony\Component\Yaml\Yaml;

$jsonEncoded = '{"accepted":{"number_of_order":{"consition":"between_number","value":"0&5"}}}';

$array = json_decode($jsonEncoded, true);

print Yaml::dump($array);

要在 ruby​​ 中加载 php 输出 yaml,可以使用相同的 yaml

require 'yaml'
yaml_str = ... # output generate from Symfony Yaml::dump
yaml_hash = YAML.load(yaml_str)

要在 php 中加载 ruby​​ 输出 yaml,我们可以使用相同的 Symfony Yaml

$yaml_str = ...; # output generate from ruby rhash.to_yaml
$yaml_associated_array = Yaml.parse(yaml_str);

将 ruby​​ 哈希加载到 php

$data = "
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    condition: between_number
    value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
";

print $data;

$data = str_replace("---", "", preg_replace("/!ruby.*Access/", "", $data));
print $data;

$result = Yaml::parse($data);
var_dump($result);

同样可以在 ruby​​ 中实现

data = %Q(
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  number_of_order: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    condition: between_number
    value: 0&5
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
)

result = str.gsub(/!ruby.*Access/, '').gsub("--- ", "")

puts result

puts YAML.load(result)

注意:我们也可以使用其他库

【讨论】:

  • 您好,@Chandan 谢谢您的回答,但我必须将整个代码放在 PHP laravel 的单个文件中。我正在使用Symfony Yaml 库来转储数组和解析数组。
  • @HitenGal 共享代码是 ruby​​ 和 php 的示例,主要代码只有 2 行,可以从 yaml 中删除不需要的行。
  • 所以你基本上是在告诉我,如果我刚刚写了use Symfony\Component\Yaml\Yaml; $jsonEncoded = '{"accepted":{"number_of_order":{"consition":"between_number","value":"0&5"}}}'; $array = json_decode($jsonEncoded, true); print Yaml::dump($array); 这段代码,那么我会得到预期的输出吗?如果您可以通过 GitHub 或任何其他方式与我分享代码,以便我可以轻松理解。谢谢
  • 但是先生,我可以在不编写任何 ruby​​ 代码的情况下做到这一点吗?因为我不想创建任何 ruby​​ 文件或编写任何 ruby​​ 代码。我必须在 PHP 中完成所有这些过程。
【解决方案2】:

我刚刚找到了我的问题的答案,我正在与大家分享。

Laravel 代码:

$jsonEncoded = '{"accepted":{"first_order_number":{"consition":"between_date","value":"2021-11-28 00:00:00&2021-11-22 00:00:00"}},"removed":{}}';
    
$process = new Process(['ruby', 'convert_hashActiveSupport.rb'],null,null, $jsonEncoded);

$process->run();
    
// executes after the command finishes
if (!$process->isSuccessful()) {
   throw new ProcessFailedException($process);
}
    
$data = $process->getOutput();

Ruby 代码:

require 'yaml'
require 'json'
require "active_support/core_ext/hash/indifferent_access"

yaml_str = "#{ ARGF.read }"

parsed_output = JSON.parse(yaml_str)
yaml_output   = parsed_output.with_indifferent_access.to_yaml
puts yaml_output

laravel 需要的包:

composer require symfony/process

ruby 所需的包:

gem install activesupport
gem install 'yaml'

在这里我得到了我的预期输出:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
accepted: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  first_order_date: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    consition: between_date
    value: 2021-11-28 00:00:00&2021-11-22 00:00:00
removed: !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2021-10-16
    • 1970-01-01
    相关资源
    最近更新 更多