栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将JSON转换为哈希,搜索和更改值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将JSON转换为哈希,搜索和更改值

我了解您的JSON可能如下所示:

"{"features":{"additional-options":true},"values":{"lo-value":34,"hi-value":554},"persons":[{"name":"john","member":true,"current":false,"sponsor":"pete","profile":"","credits":["04"],"linked":["philip","guy"],"maptools":["crossfit","soccer","running"]},{"name":"mary","member":true,"current":false,"sponsor":"judy","profile":"","credits":["all"],"activities":["swimming","cycling","running"]}],"data_map":[1122,3234]}"

我建议使用OpenStruct来组织数据:

your_struct_name =  JSON.parse(yourJson, object_class: OpenStruct)

然后,您得到了所有想要的东西。对于您显示的操作:

#change_key(hash, "features.additional-options", false)your_struct_name.features['additional-options'] = false #this one above you set in this hash-like manner because of the '-' in the middle of the key. Otherwise you could just do your_struct_name.features.additional_options = false#del_from_array(hash, "persons.name=mary.activities", "cycling")your_struct_name.persons.last.activities.delete('swimming')# or selecting by name:your_struct_name.persons.select {|person| person.name == 'mary' }.first.activities.delete('swimming')#add_to_array(hash, "persons.name=mary.activities", "hockey")your_struct_name.persons.last.activities << 'hockey'#del_key(hash, "data_map")your_struct_name.delete_field('data_map')#del_key(hash, persons.name=john.profile)...#del_key(hash, persons.name=mary.credits)...

然后,进行更改后,可以使用:

your_struct_name.to_h.to_json

您还可以使用该方法

as_json
来获得与问题所显示的结构非常相似的结构:

your_struct_name.as_json

OpenStruct
处理 结构变化的
数据非常好。如果您具有可以“建模”的数据,可以调用的名称,可以预测的某些属性,甚至可以用于此数据的方法,那么建议您创建一个Class来描述此数据,其属性和属性(甚至可以继承自OpenStruct)。然后在这个Class域内工作,创建一个抽象层。这样,您的代码将变得更加健壮和易读。不要忘记创建自动测试!它为您节省了大量时间。

组织和抽象数据的方式,特别是命名实体的方式,对代码质量有很大影响。

有关更多信息,请参见:Object和ActiveData。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386798.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号