7.Core Json – JSON 和 Ruby on Rails

最后更新于:2022-04-01 05:31:00

| API | SOURCE | | --- | --- | | Google GSON | [http://code.google.com/p/google-json/](http://code.google.com/p/google-json/) | | SOJO | [http://sojo.sourceforge.net/](http://sojo.sourceforge.net/) | | org.json (by Douglas Crockford) | [http://www.json.org/java](http://www.json.org/java) | | json-lib | [http://sourceforge.net/projects/json-lib/](http://sourceforge.net/projects/json-lib/) | | json-io | [http://code.google.com/p/json-io](http://code.google.com/p/json-io) | | jsontools | [http://jsontools.berlios.de/](http://jsontools.berlios.de/) | | jsonbeans | [http://code.google.com/p/jsonbeans/](http://code.google.com/p/jsonbeans/) | | ActiveSupport JSON | [http://api.rubyonrails.org/classes/ActiveSupport/JSON.html](http://api.rubyonrails.org/classes/ActiveSupport/JSON.html) | | Yajl | [https://github.com/brianmario/yajl-ruby](https://github.com/brianmario/yajl-ruby) | | Oj | [https://github.com/ohler55/oj](https://github.com/ohler55/oj) | Ruby on Rails提供了额外的功能,这使得转换Ruby对象为JSON更容易。下面的控制器使用ActionController的渲染方法输出地址对象到JSON: ~~~ class Person attr_accessor :first_name, :last_name def initialize(first_name=nil, last_name=nil) @first_name = first_name @last_name = last_name end end class MyController < ApplicationController def index person = Person.new('John', 'Doe') respond_to do |format| format.html # index.html.erb format.json { render :json => person} end end end ~~~ Rails的应用控制器负责编写对象为JSON格式或者解析JSON文件为对象。所以没有必要在这里写to_json方法
';