I using ruby on rails. I have a string that needs to converted to dig readable format
"search.user[0].user_id" convert this to [:search, :user,0, :user_id]
i am trying to use
"search.user[0].user_id".split('.').map(&:to_sym) which result to [:search, :user[0], :user_id]
How can i further split array index, string can position array user[0] at any position.
To parse your expression you can use:
"search.user[0].user_id".split(/]?\.|\[/).map{|k| k =~ /\A\d+\z/ && k.to_i || k.to_sym }
# => [:search, :user, 0, :user_id]
For a more generic approach also look at jsonpath (that parses more complex paths like $.store.book[*](category,author)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加