我有一个OpenStruct对象,需要转换为JSON数据。
样本哈希(来自RSPEC帮助器):
def test_order
{
"id": 505311428702,
"email": "test@gmail.com",
"closed_at": "",
"discount_codes": {
"id": 507328175,
"text": "test"
}
}
end
我正在使用下面的递归函数:
def to_recursive_ostruct(hash)
OpenStruct.new(hash.each_with_object({}) do |(key, val), memo|
memo[key] = val.is_a?(Hash) ? to_recursive_ostruct(val) : val
end)
end
对于to_recursive_ostruct(test_order),将返回:
<OpenStruct id=505311428702, email="test@gmail.com", closed_at="", ...>
使用 OpenStructObject.marshal_dump 进行转换后:
{
:id=>505311428702, :email=>"test@gmail.com", :closed_at=>"",
discount_codes=>#<OpenStruct id=507328175, text= "test">}
}
OpenStructObject.marshal_dump 在第一级为我提供了正确的数据
我还希望转换嵌套数据。
我真正需要的是:
{:id=>505311428702, :email=>"test@gmail.com", :closed_at=>"", :discount_codes=>{:id=>507328175, :text=> "test"} }
请帮助,谢谢。
答案 0 :(得分:2)
签出docs。
您可以使用OpenStruct#marshal_dump
:
<div>
{/* item is object with user's name and its other details on it */}
{items.map((item, index) => {
return <div key={index}>
--note the name property is primitive--->{item.name}</div>;
})}
</div>
OpenStruct#to_h
也将起作用:
#include<stdio.h>
static const char *test(void){
const char *a = NULL;
char b[64] = {0,};
a = b;
return a;
}
int main() {
const char *a;
a = test();
}
您可以将对象转换为哈希,然后将其转换为JSON:
openstruct_object.marshal_dump
但是看起来您想要的是一个Hash对象,而不是JSON对象。
答案 1 :(得分:1)
要将深层openstruct转换为哈希,可以遵循以下几条原则:
def deep_openstruct_to_hash(object)
object.each_pair.with_object({}) do |(key, value), hash|
hash[key] = value.is_a?(OpenStruct) ? deep_openstruct_to_hash(value) : value
end
end
然后:
openstruct_object = to_recursive_ostruct(test_order)
#=> #<OpenStruct id=505311428702, email="test@gmail.com", closed_at="", discount_codes=#<OpenStruct id=507328175, text="test">>
deep_openstruct_to_hash(openstruct_object)
# {
# :id=>505311428702,
# :email=>"test@gmail.com",
# :closed_at=>"",
# :discount_codes=>{
# :id=>507328175,
# :text=>"test"
# }
# }
答案 2 :(得分:0)
也感谢这个gist: 也可以转换为哈希数组。
def recursive_ostruct(object)
case object
when Hash
hash = {}; object.each{|k,v| hash[k] = recursive_ostruct(v)}
OpenStruct.new(hash)
when Array
object.map {|e| recursive_ostruct(e) }
else
object
end
end
答案 3 :(得分:0)
在Ruby 2.4+上,您可以在猴子补丁中将transform_values
与递归函数一起使用。
class OpenStruct
def deep_to_h
to_h.transform_values do |v|
v.is_a?(OpenStruct) ? v.deep_to_h : v
end
end
end
或者如果您不想猴子打补丁
def deep_to_h(obj)
obj.to_h.transform_values do |v|
v.is_a?(OpenStruct) ? deep_to_h(v) : v
end
end
答案 4 :(得分:0)
以@ Andrey-Deineko,@ owyongsk,@ aldrien.h完成的工作为基础,这里是一个处理数组的转换器。尽管OP并没有特别注意,但其他人可能会发现它很有帮助。
def openstruct_to_h(object)
object.to_h.transform_values do |value|
value.is_a?(OpenStruct) ? openstruct_to_h(value) : value.is_a?(Array) ? value.map{|v| v.is_a?(String) ? v : openstruct_to_h(v) } : value
end
end
# Example usage
open_struct_object = OpenStruct.new(paramtest: "ok", array_test: [OpenStruct.new(array_one: true), OpenStruct.new(array_two: false, nested_os: OpenStruct.new(works: 'maybe'))], os_test: OpenStruct.new(testy: "yup"))
openstruct_to_h(open_struct_object)
=> {:paramtest=>"ok", :array_test=>[{:array_one=>true}, {:array_two=>false, :nested_os=>{:works=>"maybe"}}], :os_test=>{:testy=>"yup"}}