0

Core Json–JSON 和 Ruby on Rails

Posted by 撒得一地 on 2016年4月20日 in JSON
API SOURCE
Google GSON http://code.google.com/p/google-json/
SOJO http://sojo.sourceforge.net/
org.json (by Douglas Crockford) http://www.json.org/java
json-lib http://sourceforge.net/projects/json-lib/
json-io http://code.google.com/p/json-io
jsontools http://jsontools.berlios.de/
jsonbeans http://code.google.com/p/jsonbeans/
ActiveSupport JSON http://api.rubyonrails.org/classes/ActiveSupport/JSON.html
Yajl https://github.com/brianmario/yajl-ruby
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方法。

上一篇:

下一篇:

相关推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注

1 + 7 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.