如何从Ruby on Rails解析JSON数据或(JSON代码) [英] How to parse the json data or (json code) from Ruby on rails

查看:329
本文介绍了如何从Ruby on Rails解析JSON数据或(JSON代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ruby on Rails源,现在我要解析数据并发送数据的代码.在我的代码中,它将从用户那里获取名称并显示它,如何在ROR中解析数据.

I have an Ruby on rails source,code now i want to parse the data, and send the data.In my code,it will fetches the name from user and display it,How to parse the data in ROR.

这是我的controller.rb代码

This is my controller.rb code

def index
    @hotels = Hotel.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @hotels }
    end
  end

  # GET /hotels/1
  # GET /hotels/1.json
  def show
    @hotel = Hotel.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @hotel }
    end
  end

  # GET /hotels/new
  # GET /hotels/new.json
  def new
    @hotel = Hotel.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @hotel }
    end
  end

  # GET /hotels/1/edit
  def edit
    @hotel = Hotel.find(params[:id])
  end

  # POST /hotels
  # POST /hotels.json
  def create
    @hotel = Hotel.new(params[:hotel])

    respond_to do |format|
      if @hotel.save
        format.html { redirect_to @hotel, notice: 'Hotel was successfully created.' }
        format.json { render json: @hotel, status: :created, location: @hotel }
      else
        format.html { render action: "new" }
        format.json { render json: @hotel.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /hotels/1
  # PUT /hotels/1.json
  def update
    @hotel = Hotel.find(params[:id])

    respond_to do |format|
      if @hotel.update_attributes(params[:hotel])
        format.html { redirect_to @hotel, notice: 'Hotel was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @hotel.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /hotels/1
  # DELETE /hotels/1.json
  def destroy
    @hotel = Hotel.find(params[:id])
    @hotel.destroy

    respond_to do |format|
      format.html { redirect_to hotels_url }
      format.json { head :no_content }
    end
  end

如何使用Json在ROR中解析这些数据 如何为这些数据编写解析json文件,该怎么做

How to parse these data in ROR using Json HOw to write parsing json file for these data,how to do that one

推荐答案

有很多方法:

  1. 为每个模型创建方法to_json
  2. 创建名为hotels/index.json.erb的视图,并使用ERb模板引擎编写JSON代码

  1. Create method to_json for each model
  2. Create view named, i.e. hotels/index.json.erb and write JSON code using ERb templating engine

[
  <% @hotels.each do |hotel| %>
    { 'id': <%= hotel.id %>, 'name': "<%= hotel.name %>" },
  <% end %>
]

  • 使用类似 jbuilder 之类的库(页面底部是以下内容的替代列表) JBuilder)

  • Use library like jbuilder (on the bottom of page is list of alternatives to JBuilder)

    # hotels/index.json.jbuilder
    json.array!(@hotels) do |hotel|
      json.id hotel.id
      json.name hotel.name
    end
    

  • 这篇关于如何从Ruby on Rails解析JSON数据或(JSON代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆