在仅限API的应用中使用Rails Serializer时出现问题。该应用程序返回一个javascript对象,其中的键没有用双引号括起来。值得注意的是,源API返回的javascript对象的键没有被引号括起来。
我发现了两个似乎相关的帖子。第二个答案here指出,如果您收到一个不是字符串的对象,您就不需要解析它,所以我猜这是什么' s在这里,但我无法在我的模型中找出代替JSON.parse response
的内容。另一篇相关文章here;正如那里所建议的那样,我已经尝试了return JSON(response)
而不是JSON.parse response
,这也没有产生有效的JSON。我看到我可以删除Controller中的each_serializer: KoboApiSerializer
行,并尝试使用模型中的`return JSON(response)行。
对于我应该如何产生有效的JSON结果的任何建议将不胜感激。
Model,Controller,返回JSON,以及从下面的API返回的源JSON。我还通过控制器中的线路将puts
和我的终端包含在我的终端中。提前致谢。
型号:
class KoboApi < ActiveRecord::Base
require 'rest_client'
USERNAME = ENV['KOBO_API_USER']
PASSWORD = ENV['KOBO_API_PWD']
SURVEY = ENV['KOBO_API_SURVEY']
API_BASE_URL = ENV['API_BASE_URL']
def self.get_api_info
uri = "#{API_BASE_URL}/data/#{SURVEY}?format=json"
rest_resource = RestClient::Resource.new(uri, USERNAME, PASSWORD)
response = rest_resource.get
JSON.parse response
end
end
控制器:
class KoboDataController < ApplicationController
def index
@kobo_info = KoboApi.get_api_info
puts "@kobo_info: #{@kobo_info.inspect}" #values for this returned below. this shows keys surround with quotes.
@kobo_info.each do |kobo_info|
#some records have NULL for the month_and_year date field. this if then statement
date = if kobo_info['month_and_year']
Date.parse(kobo_info['month_and_year'])
else
nil
end
KoboApi.where(lemurs_quantity: kobo_info['lemurs_quantity'], month_and_year: date, lemur_category: kobo_info['lemur_category'], location_admin1: kobo_info['location_admin1'], location_admin2: kobo_info['location_admin2']).first_or_create
end
render(
json.KoboApi.all,
each_serializer: KoboApiSerializer
)
end
end
从我的Rails API应用程序返回的对象示例
有一个序列化器(这里是&#34; kobo_data&#34;),因此对象在其下分组。我包含了返回的示例对象 - 请注意,此处的对象不一定与我从源API或控制台发布的对象相匹配 - 包括它以显示密钥未包含在引号中。
{
kobo_data: [
{
lemurs_quantity: 20,
month_and_year: "2016-03-01",
lemur_category: "indri",
location_admin1: "toliara",
location_admin2: "androy"
}
]
}
来自API的源json示例:
源API返回的一个对象(众多对象)。此处的值与上方或下方的值不匹配 - 仅包括显示密钥未用双引号括起来。
[
{
location_admin1: "fianarantsoa",
location_admin2: "atsimo-atsinanana",
lemurs_quantity: "5",
month_and_year: "2016-01-01",
lemur_category: "brown_lemur",
},
]
将它放到终端....
值puts
通过控制器中的行显示到我的终端,显示它似乎是一个有效的JSON对象,带有引号中的键。如上所述,此处的值与源数据中的值不匹配 - 这只是为了显示此处返回的值包含在引号中。
Processing by KoboDataController#index as HTML
@kobo_info: [{"location_admin1"=>"fianarantsoa", "location_admin2"=>"atsimo-atsinanana","lemurs_quantity"=>"5", "month_and_year"=>"2016-01-01","lemur_category"=>"brown_lemur"}
KoboApi Load (0.3ms) SELECT "kobo_apis".* FROM "kobo_apis" WHERE "kobo_apis"."lemurs_quantity" = $1 AND "kobo_apis"."month_and_year" = $2 AND "kobo_apis"."lemur_category" = $3 AND "kobo_apis"."location_admin1" = $4 AND "kobo_apis"."location_admin2" = $5 ORDER BY "kobo_apis"."id" ASC LIMIT 1 [["lemurs_quantity", 5], ["month_and_year", "2016-01-01"], ["lemur_category", "brown_lemur"], ["location_admin1", "fianarantsoa"], ["location_admin2", "atsimo-atsinanana"]]
答案 0 :(得分:0)
将此归结为缺乏经验。一切都运行正常 - 事实证明我正在查看浏览器显示的响应,而不是查看源。详情如下。
在浏览器中显示
{
kobo_data: [
{
lemurs_quantity: 1,
month_and_year: "2013-06-01",
lemur_category: "no_response",
location_admin1: "antsiranana",
location_admin2: "diana"
}, {
lemurs_quantity: 1,
month_and_year: "2013-06-01",
lemur_category: "no_response",
location_admin1: "antsiranana",
location_admin2: "diana"
},
]
}
<强> VS。来源
{
"kobo_data": [
{
"lemurs_quantity": 1,
"month_and_year": "2013-06-01",
"lemur_category": "no_response",
"location_admin1": "antsiranana",
"location_admin2": "diana"
}, {
"lemurs_quantity": 1,
"month_and_year": "2013-06-01",
"lemur_category": "no_response",
"location_admin1": "antsiranana",
"location_admin2": "diana"
},
]
}