gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:16:26 28 4
gpt4 key购买 nike

我正在尝试为一个副项目创建一个 super 简单的 JSON 网络服务。但是,我在将我的对象转换为 JSON 时遇到了一些问题,有人可以帮助我吗?

我有以下类(class):

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

最佳答案

这应该可以解决:

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 字符串作为 Location 并将其编码为 json。这会在 json 字符串中产生一个 json 字符串,这就是为什么有很多 '\' - 用作转义字符的原因。

关于Ruby to_json 在用引号引起来的对象上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855390/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com