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

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

如题,简单举个例子有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包吗?

共收到 3 条回复
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不就可以分出来了吗

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