JSON::P arserError in ...意外标记在 '../../test.json' - RUBY ON RAILS



我已经纠结了一段时间了。我有JSON (test.json):

[
{
"title" : "Star Wars",
"Durée" : "3h",
"Date" : "6 Mars",
"Ratio" : "4.5/5"
},
{
"title" : "Top Gun",
"Durée" : "2h",
"Date" : "17 Mai",
"Ratio" : "4.3/5"
}]

我有一个控制器(food_controller.rb):

class FoodController < ApplicationController
def index
require "json"
file = File.join "../../test.json"
data = JSON.parse(file)

end

结束,我有这个错误:输入图片描述

如果你的json文件在项目的根目录下,那么试试这个:

require "json"
class FoodController < ApplicationController
def index
data = JSON.parse(File.read('test.json'))
end
end

否则,如果你的文件在某个目录e.g. tmp中,那么只需将该目录名称作为json文件的前缀添加到

data = JSON.parse(File.read('tmp/test.json'))

相关内容

最新更新