我有一個陣列來存盤一些這樣的資料,這是我正在使用的確切輸入
{
"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?? 哈希訪問,但沒有任何效果。
基本上,我試圖扭轉這個問題
如何從 ruby?? 哈希訪問 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);
uj5u.com熱心網友回復:
要在 ruby?? 中將哈希轉換為 yaml,我們可以使用yaml庫
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 輸出 yamlyaml可以使用相同的庫
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)
注意:我們也可以使用其他庫
uj5u.com熱心網友回復:
我剛剛找到了我的問題的答案,我正在與大家分享。
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();
紅寶石代碼:
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 {}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359991.html
