新手问题 ruby 如何实现对文件的分割?

GSTA_Yalo · 2019年04月11日 · 最后由 varma 回复于 2019年04月12日 · 1335 次阅读

如题,简单举个例子有 json 文件内容为:[ { "kind": "Service", "apiVersion": "v1", "metadata": { "name": "redis-slave", "namespace": "development3", "selfLink": "/api/v1/namespaces/default/services/redis-slave", "uid": "bdb80a8f-db93-11e4-b293-f8b156af4ae1",

"creationTimestamp": "2015-04-05T13:00:31Z", "labels": { "name": "redis", "role": "slave" } }, "spec": { "ports": [ { "name": "", "protocol": "TCP", "port": 6379, "targetPort": "redis-server" } ], "selector": { "name": "redis", "role": "slave" }, "clusterIP": "10.97.0.141", "sessionAffinity": "None" }, "status": {} }, { "kind": "Endpoints", "apiVersion": "v1", "metadata": { "name": "myendpoint", "namespace": "default", "selfLink": "/api/v1/namespaces/default/endpoints/myendpoint", "uid": "59d05b48-dadb-11e5-937e-18037327aaeb", "creationTimestamp": "2016-02-24T09:45:34Z" }, "subsets": [ { "addresses": [ { "ip": "172.17.0.25" } ], "ports": [ { "name": "https", "port": 6443, "protocol": "TCP" } ] } ] } ] ,现要按照变量“kind”将这个文件中的内容划分为多个 hash 串,请问怎么实现?有相应的 gem 包吗?

require 'json'

json = '[  { "kind": "Service", "apiVersion": "v1", "metadata": { "name": "redis-slave", "namespace": "development3", "selfLink": "/api/v1/namespaces/default/services/redis-slave", "uid": "bdb80a8f-db93-11e4-b293-f8b156af4ae1",
"creationTimestamp": "2015-04-05T13:00:31Z", "labels": { "name": "redis", "role": "slave" } }, "spec": { "ports": [ { "name": "", "protocol": "TCP", "port": 6379, "targetPort": "redis-server" } ], "selector": { "name": "redis", "role": "slave" }, "clusterIP": "10.97.0.141", "sessionAffinity": "None" }, "status": {} },{ "kind": "Endpoints", "apiVersion": "v1", "metadata": { "name": "myendpoint", "namespace": "default", "selfLink": "/api/v1/namespaces/default/endpoints/myendpoint", "uid": "59d05b48-dadb-11e5-937e-18037327aaeb", "creationTimestamp": "2016-02-24T09:45:34Z" }, "subsets": [ { "addresses": [ { "ip": "172.17.0.25" } ], "ports": [ { "name": "https", "port": 6443, "protocol": "TCP" } ] } ] } ] '
JSON.parse(json).group_by {|h| h["kind"]}

# 结果
 {"Service"=>[{"kind"=>"Service", "apiVersion"=>"v1", "metadata"=>{"name"=>"redis-slave", "namespace"=>"development3", "selfLink"=>"/api/v1/namespaces/default/services/redis-slave", "uid"=>"bdb80a8f-db93-11e4-b293-f8b156af4ae1", "creationTimestamp"=>"2015-04-05T13:00:31Z", "labels"=>{"name"=>"redis", "role"=>"slave"}}, "spec"=>{"ports"=>[{"name"=>"", "protocol"=>"TCP", "port"=>6379, "targetPort"=>"redis-server"}], "selector"=>{"name"=>"redis", "role"=>"slave"}, "clusterIP"=>"10.97.0.141", "sessionAffinity"=>"None"}, "status"=>{}}], "Endpoints"=>[{"kind"=>"Endpoints", "apiVersion"=>"v1", "metadata"=>{"name"=>"myendpoint", "namespace"=>"default", "selfLink"=>"/api/v1/namespaces/default/endpoints/myendpoint", "uid"=>"59d05b48-dadb-11e5-937e-18037327aaeb", "creationTimestamp"=>"2016-02-24T09:45:34Z"}, "subsets"=>[{"addresses"=>[{"ip"=>"172.17.0.25"}], "ports"=>[{"name"=>"https", "port"=>6443, "protocol"=>"TCP"}]}]}]}

这个符合你的要求吗

varma 回复

感谢答复,group_by 方法处理过后还是一个 hash,如何将这个 hash 继续划分为两个独立的 hash,如: {"Service"=>[{"kind"=>"Service",...},{""Endpoints"=>[{"kind"=>"Endpoints",......},并分别命名。

GSTA_Yalo 回复

对结果用 each 不就可以分出来了吗

需要 登录 后方可回复, 如果你还没有账号请 注册新账号