Parse Json IN Ruby

Source: Internet
Author: User

The Ruby language is widely used and can be used in a simple and flexible way to help us implement many functional requirements. Ruby parses Json in a very simple way.

  • Ruby special syntax concepts
  • Example of Ruby code for getting the current class name
  • Ruby module OpenURI to get http/FTP address content
  • Analysis of Ruby encryption implementation code examples
  • Ruby blocks provides flexible encoding methods

Example of Json parsing IN Ruby:

  1. Json = '["a", "B", "C"]'
  2. Puts "Unsafe # {unsafe_json
    (Json). inspect }"
  3. # Output Unsafe
    ["A", "B", "C"]

Ruby parses Json to parse the preceding json string into an Array. This method is not safe, for example:

 
 
  1. json = 'puts "Danger 
    Will Robinson"' 
  2. puts "Unsafe #{unsafe_json
    (json).inspect}"  

What should I output? Unfortunately, nothing can be parsed. A warning is displayed: warning: character class has '[' without escape security method is as follows:

 
 
  1. module SafeJSON   
  2. require 'monitor'   
  3. def SafeJSON.build_safe_json   
  4. ret = nil   
  5. waiter = ''   
  6. waiter.extend(MonitorMixin)   
  7. wait_cond = waiter.new_cond   
  8. Thread.start do   
  9. $SAFE = 4   
  10. ret = Proc.new {|json|   
  11. eval(json.gsub(/(["'])/s*:/s*
    (['"0-9tfn/[{])/){"#{$1}=>#{$2}"}) }   
  12. waiter.synchronize do wait_cond.signal   
  13. end   
  14. end  
  15. waiter.synchronize do wait_
    cond.wait_while { ret.nil? } end   
  16. return ret   
  17. end   
  18. @@parser = SafeJSON.build_safe_json   
  19. # Safely parse the JSON input   
  20. def SafeJSON.parse(input)   
  21. @@parser.call(input)   
  22. rescue SecurityError   
  23. return nil   
  24. end  
  25. end  

With this Module, you can use Ruby to parse Json:

 
 
  1. Using les = SafeJSON. parse ('
    {"Les": [{"name": "site120 ","
    Email ":" site120@163.com "," sex ":" male "},
    {"Name": "site120_2", "email": "site1
    20@163.com _ 2 "," sex ":" male _ 2 "}]} ')
  2. Puts cmdles ["cmdles"] [1] ["name"]
    # Output site120_2

Rails has built-in AJAX support through RJS. There may not be many opportunities to use json. However, as a convenient data exchange format, pay attention to the value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.