简体   繁体   English

如何根据内部哈希中的键值最有效地按顺序遍历哈希哈希?

[英]How can I most efficiently iterate over a hash of hashes in order based on key value in inner hash?

I've got a JSON hash of hashes returned by a website API that I want to parse and display based on a specific key's value within the internal hashes. 我有一个网站API返回的哈希值的JSON哈希值,我想根据内部哈希值中特定键的值来解析和显示该哈希值。

I can think of solutions that would achieve this, but they would take a number of lines of code and don't seem efficient. 我可以想到可以实现此目的的解决方案,但是它们会花费很多行代码,而且效率似乎不高。 Surely there must be a way to natively in Rails, given the focus on convention over configuration. 考虑到约定优于配置,肯定有一种方法可以在Rails中进行本地化。 I've googled around a bit, but found nothing that covers this issue. 我已经在Google上搜索了一下,但没有发现任何涵盖此问题的信息。

Sample Response from API: 来自API的示例响应:

[{"banner":"01197271","birthday":"1991-01-11","committee_id":1,"created_at":"2012-08-08T01:56:02-05:00","email":"me@example.com","first_name":"Dan","graduation_date":"May 2013","hometown":"San Antonio","hours_enrolled":15,"id":2,"image":{"url":null,"thumb":{"url":null},"large":{"url":null}},"invitation_accepted_at":null,"invitation_limit":null,"invitation_sent_at":null,"invitation_token":null,"invited_by_id":null,"invited_by_type":null,"last_name":"Tester","local_apt":"","local_city":"San Antonio","local_state":"Texas","local_street":"One UTSA Circle","local_zip":"78249","major":"Computer Science","permanent_apt":"","permanent_city":"","permanent_state":"","permanent_street":"One UTSA Circle","permanent_zip":"","phone":"5558813284","same_address":true,"tour_trained":false,"updated_at":"2012-08-17T03:35:26-05:00","utsa_id":"uoi431"},
{"banner":"","birthday":"1990-10-25","committee_id":null,"created_at":"2012-08-03T16:19:23-05:00","email":"you@example.com","first_name":"Test","graduation_date":null,"hometown":null,"hours_enrolled":null,"id":1,"image":{"url":null,"thumb":{"url":null},"large":{"url":null}},"invitation_accepted_at":null,"invitation_limit":null,"invitation_sent_at":null,"invitation_token":null,"invited_by_id":null,"invited_by_type":null,"last_name":"User","local_apt":"","local_city":"","local_state":"","local_street":"","local_zip":"","major":null,"permanent_apt":"","permanent_city":"","permanent_state":"","permanent_street":"","permanent_zip":"","phone":"","same_address":false,"tour_trained":false,"updated_at":"2012-08-15T10:05:54-05:00","utsa_id":""}]

Potential solution would be to go through each internal hash, determining value of relevant key value, then store, based on where the key value places it compared to already tested hashes. 可能的解决方案是遍历每个内部哈希,确定相关键值的值,然后根据键值与已测试哈希值相比的位置存储。 When complete, return. 完成后,返回。

Ok so if you have objects that are set up to parse this information, those objects can build themselves based off the parameters of your hash. 好吧,如果您有一些对象被设置为可以解析此信息,则这些对象可以根据哈希参数建立自己。 So you could do something like this 所以你可以做这样的事情

object = MyObject.create(your_hash_parameters)

Where your_hash_parameters are the parameters that you presented in your example. 其中your_hash_parameters是示例中显示的参数。

I'm not sure what would happen if there were more paramaters than your object knew what to do with, if it would still build itself or not. 我不确定如果参数多于您的对象知道该如何处理(如果它仍然可以自行构建),将会发生什么。 If that is the case, you could use the delete_if method to exclude attributes that are unwanted. 在这种情况下,可以使用delete_if方法排除不需要的属性。

One more note, if this isn't something that you want saved to your database, and its only to display temporary information. 还有一点需要注意的是,如果这不是您想要保存到数据库中的内容,并且仅用于显示临时信息。 I would set up a model with attr_accessors that represent the attributes that you are displaying. 我将使用attr_accessors设置一个模型,该模型代表您正在显示的属性。

如评论所述,我将创建一个ActiveResource对象并向其中添加相关方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM