- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试为一个副项目创建一个 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/
我有一个 Ruby 脚本,可以将哈希值转换为 json 并将其写入文件。我使用 to_json 方法。 在文件中,我得到以下结果: {"name":"Bob","age":42} 我将此脚本复制到我的
如何将to_json输出中的字段值由null改为""? 它目前返回 {"name":"priya","mobile":null} 我想要 {"name":"priya","mobile":""} 或
我正在使用 Sinatra 并尝试通过使用“json”gem 并调用 .to_json 方法以 JSON 格式输出对象。我希望输出为 JSON,其中包含 attr_reader 部分中的符号及其值。
我正在尝试为一个副项目创建一个 super 简单的 JSON 网络服务。但是,我在将我的对象转换为 JSON 时遇到了一些问题,有人可以帮助我吗? 我有以下类(class): class Locati
我有一个 Python 代码可以正确处理一个包含 20 行的文件,但是当我尝试发送包含 60.000 条记录的同一文件时,它显示“108936 段错误”。 有什么办法可以避免这个错误,这是我的机器吗?
我写了一个 plpgsql 脚本,它在一个字符串中生成一个 json 对象数组,但是在我使用 to_json() 方法将一个带有该字符串的变量传递给它之后,它返回一个双引号的结果,并且每个双引号字符都
我正在尝试从嵌套的 pyspark DataFrame 生成一个 json 字符串,但丢失了键值。我的初始数据集类似于以下内容: data = [ {"foo": [1, 2], "bar":
我想这样做: y = 0.2 => y = 0.200 x = 0.24524452 => x = 0.245 我提出了以下请求,在逗号后面保留 3 个数字 pd.opt
我有一个包含电影数据的“.csv”文件,我正在尝试将其重新格式化为 JSON 文件以在 MongoDB 中使用它。所以我将该 csv 文件加载到 pandas DataFrame 中,然后使用 to_
在我的 Controller 中: @arr = [] @jsonPosts = Post.each do |p| arrLatLng = {} arrLatLng["latitude"] =
我在使用 bsoncxx 将 bson 文档转换为 json 字符串时遇到困难。bsoncxx::to_json 函数返回一个“无效”/损坏的 std::string 对象。我无法读取其中的字符,当
我在处理 JSON 字符串输出时遇到问题。我正在使用制表符分隔的 CSV 文件,它看起来像这样: date time loc_id country name sub
出于某种原因,pandas to_json 函数正在将数据帧的索引转换为字符串。有没有办法避免这种情况? >>> import pandas as pd >>> df = pd.DataFrame({
dataframe中日期的原始形式是: Date 2018-09-1
我正在构建一个基于 Sinatra 的 API,我发现我的用户模型有一些奇怪的响应。每个响应都会返回一个充满空值的集合。其他模型按预期返回。 irb(main):018:0> User => User
我有一个 pandas 系列,其中包含从日-月-年字符串创建的日期时间对象 series = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000']) se
我注意到了这种行为,但不确定这是一个错误。我创建了一个包含 2 个整数列和 1 个浮点列的数据框 import pandas as pd df = pd.DataFrame([[1,2,0.2],[3
首先,我没有使用 Rails。我在这个项目中使用 Sinatra 和 Active Record。 我希望能够在我的模型类上覆盖 to_json 或 as_json 并让它定义一些“默认”选项。例如我
我正在尝试实现 wysihml5在使用 Activerecord 的 sinatra 应用程序中。 富文本编辑器运行良好,当我提交表单时,我将正确的 html 发布到 Controller : pry
我有一个使用 Ruby 标准库中的 Set 类的数据结构。我希望能够将我的数据结构序列化为 JSON 字符串。 默认情况下,Set 序列化为数组: >> s = Set.new [1,2,3] >>
我是一名优秀的程序员,十分优秀!