简体   繁体   English

Ruby to_json 包裹在引号中的对象

[英]Ruby to_json on objects wrapped in quote's

I'm trying to create a super easy JSON webservice for a side-project.我正在尝试为一个辅助项目创建一个超级简单的 JSON 网络服务。 However I'm having some trouble converting my objects to JSON, could someone please help me out?但是我在将我的对象转换为 JSON 时遇到了一些麻烦,有人可以帮我吗?

I have the following classes:我有以下课程:

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

and

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

Given a random input I would like the output to be something like this:给定一个随机输入,我希望 output 是这样的:

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

However the ouput I'll get is this:但是我会得到的输出是这样的:

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

Could someone please explain me how I can fix this?有人可以解释一下我该如何解决这个问题吗?

EDIT:编辑:

I'm using a Sintra webservice which looks something like this:我正在使用一个看起来像这样的 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

If you try this:如果你试试这个:

@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"
}

And do this in your view在你看来这样做

raw(@object)

You should be alright:)你应该没事:)

edit编辑

#controller
@object = Model.all

view
raw(@object.to_json)

will also work...也将工作...

This should fix it:这应该解决它:

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

Your problem was that in your to_json in Spot you were using the json string for Location and encoding that into json.您的问题是,在 Spot 中的 to_json 中,您使用 json 字符串作为 Location 并将其编码为 json。 This causes a json string within the json string which is why there were lots of '\'s - used as escape characters.这会导致 json 字符串中出现 json 字符串,这就是为什么有很多 '\'s - 用作转义字符的原因。

I believe your problem is that to_json method in your Location class already returns a string:我相信您的问题是您的 Location class 中的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

And later, in your Spot class, your @location is evaluted as a string后来,在您的Spot class 中,您的 @location 被评估为一个字符串

If you using inspect or getting the output from irb, ie:如果您使用检查或从 irb 获取 output,即:

irb> object.to_json

Try using puts尝试使用puts

irb> object.to_json.puts

then those slashes should disappear.那么那些斜线应该消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM