用引号括起来的对象上的Ruby to_json

时间:2011-05-02 09:14:39

标签: ruby json

我正在尝试为侧面项目创建一个超级简单的JSON Web服务。但是我在将对象转换为JSON时遇到了一些麻烦,有人可以帮帮我吗?

我有以下课程:

class Location
  attr_reader :street, :city, :state, :country, :zip, :latitude, :longitude

  def initialize(street, city, state, country, zip, latitude, longitude)
    @street = street
    @city = city
    @state = state
    @country = country
    @zip = zip
    @latitude = latitude
    @longitude = longitude
  end

  def to_json
    {
      'street' => @street,
      'city' => @city,
      'state' => @state,
      'country' => @country,
      'zip' => @zip,
      'latitude' => Float(@latitude),
      'longitude' => Float(@longitude)
    }.to_json
  end
end

class Spot
  attr_reader :name, :category, :location, :id

  def initialize(id, name, category, location)
    @name = name
    @category = category
    @location = location
    @id = id
  end

  def to_json
    {
      'name' => @name,
      'category' => @category,
      'location' => @location.to_json,
      'id' => @id
    }.to_json
  end

end

给定一个随机输入,我希望输出是这样的:

{
"name":"Wirelab",
"category":"Bier",
"location":
{
    "street":"Blaatstraat 12",
    "city":"Enschede",
    "state":"Overijssel",
    "country":"Nederland",
    "zip":"7542AB",
    "latitude": 31.21312,
    "longitude":41.1209
}
,
"id":"12"
}

然而,我得到的输出是:

{
    "name":"Wirelab",
    "category":"Bier",
    "location":"
    {
        "street\":"Blaatstraat 12",
        "city\":\"Enschede\",
        \"state\":\"Overijssel\",
        \"country\":\"Nederland\",
        \"zip\":\"7542AB\",
        \"latitude\":31.21312,
        \"longitude\":41.1209
    }
    ",
    "id":"12"
}

有人可以解释一下我如何解决这个问题吗?

编辑:

我正在使用的Sintra网络服务看起来像这样:

get '/spots' do  
       #json = spots.to_json
       spot =  Spot.new("12", "Wirelab", "Bier", Location.new("Blaatstraat 12", "Enschede", "Overijssel", "Nederland", "7542AB", "31.21312", "41.1209"))
       json = spot.to_json
       if callback
         content_type :js
         response = "#{callback}(#{json})" 
      else
         content_type :json
         response = json
       end
  response
end

4 个答案:

答案 0 :(得分:2)

如果您尝试这样做:

@object = {
"name":"Wirelab",
"category":"Bier",
"location":
{
    "street":"Blaatstraat 12",
    "city":"Enschede",
    "state":"Overijssel",
    "country":"Nederland",
    "zip":"7542AB",
    "latitude": 31.21312,
    "longitude":41.1209
}
,
"id":"12"
}

并在您的视图中执行此操作

raw(@object)

你应该没问题:)

修改

#controller
@object = Model.all

view
raw(@object.to_json)

也会有用......

答案 1 :(得分:2)

这应该解决它:

class Location
  attr_reader :street, :city, :state, :country, :zip, :latitude, :longitude

  def initialize(street, city, state, country, zip, latitude, longitude)
    @street = street
    @city = city
    @state = state
    @country = country
    @zip = zip
    @latitude = latitude
    @longitude = longitude
  end

  def to_hash
    {
      'street' => @street,
      'city' => @city,
      'state' => @state,
      'country' => @country,
      'zip' => @zip,
      'latitude' => Float(@latitude),
      'longitude' => Float(@longitude)
    }
  end

  def to_json
    self.to_hash.to_json
  end
end

class Spot
  attr_reader :name, :category, :location, :id

  def initialize(id, name, category, location)
    @name = name
    @category = category
    @location = location
    @id = id
  end

  def to_hash
    {
      'name' => @name,
      'category' => @category,
      'location' => @location.to_hash,
      'id' => @id
    }
  end

  def to_json
    self.to_hash.to_json
  end
end

你的问题是你在Spot中的to_json中使用json字符串进行定位并将其编码到json中。这导致json字符串中的json字符串,这就是为什么有很多'\' - 用作转义字符。

答案 2 :(得分:1)

我相信你的问题是你的Location类中的to_json方法已经返回一个字符串:

def to_json
    {
      'street' => @street,
      'city' => @city,
      'state' => @state,
      'country' => @country,
      'zip' => @zip,
      'latitude' => Float(@latitude),
      'longitude' => Float(@longitude)
    }.to_json  # Here, this makes it to return a string
  end
end

稍后,在您的Spot课程中,您的@location将被评估为字符串

答案 3 :(得分:0)

如果您使用检查或从irb获取输出,即:

irb> object.to_json

尝试使用puts

irb> object.to_json.puts

然后那些斜线应该消失。