I'm trying to load images from Flickr's API into a Ruby on Rails app, but I'm getting "Unexpected Token" on my JSON.parse() line.
我正在尝试将Flickr的API中的图像加载到Ruby on Rails应用程序中,但我在JSON.parse()行上获得了“Unexpected Token”。
I found another response here where the returned JSON had it's double quotes escaped out, and the solution was to add the .gsub thing to the end, but I'm still getting an error.
我在这里找到了另一个响应,其中返回的JSON将其双引号转义出来,解决方案是将.gsub事件添加到最后,但我仍然收到错误。
Anyone know what the problem is?
谁知道问题是什么?
def add
@jsonresults = open("http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=bb398c11934abb6d51bdd720020f6a4a&per_page=1&page=1&format=json&nojsoncallback=1").read
@images = JSON.parse(@jsonresults.to_json.gsub('\"', '"'))
end
The error:
JSON::ParserError in ImagesController#add
757: unexpected token at '"{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605@N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}"'
2 个解决方案
#1
5
The json returned by the call looks fine. Change your parsing to this:
通话回来的json看起来很好。将解析更改为:
@images = JSON.parse(@jsonresults)
#2
1
That is not valid JSON. The outer set of double-quotes do not belong. This is the valid version:
这不是有效的JSON。外部双引号组不属于。这是有效版本:
'{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605@N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}'
#1
5
The json returned by the call looks fine. Change your parsing to this:
通话回来的json看起来很好。将解析更改为:
@images = JSON.parse(@jsonresults)
#2
1
That is not valid JSON. The outer set of double-quotes do not belong. This is the valid version:
这不是有效的JSON。外部双引号组不属于。这是有效版本:
'{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605@N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}'