Tag: json

如何将rails模型导出到json模式?

我希望优化我们为一些模型构建表单的方式,理想情况下我想从json-schema构建它们。 是否有一个gem或类似的东西可以让我将模型定义导出到json-schema? 奖励:通过validation。 奖励:在建立关联关系时。

将Ruby对象序列化为JSON并返回?

我想将一个对象序列化为JSON,将其写入文件并将其读回。 现在我希望在.net中你有json.net之类的东西,你可以这样做: JsonSerializer.Serialize(obj); 并完成它。 你得到了JSON字符串。 我如何在Ruby中执行此操作? 没有Rails,没有ActiveRecord,没有任何东西。 有没有找不到的gem? 我安装了JSON gem并调用: puts JSON.generate([obj]) 其中obj是一个像这样的对象: class CrawlStep attr_accessor :id, :name, :next_step def initialize (id, name, next_step) @id = id @name = name @next_step = next_step end end obj = CrawlStep.new(1, ‘step 1’, CrawlStep.new(2, ‘step 2’, nil)) 我得到的全部是: [“#”] 我究竟做错了什么?

Rails中奇怪的JSON Javascript问题

我正试图从我的控制器获取我的JSON到我的视图。 在我的控制器中我正在做: @nodes = Node.all @json = @nodes.as_json(:only => [:ID, :Lat, :Lon]) 在我看来,我尝试过: 1) var stuff = 2) var stuff = 3) var stuff = 所有这些都给了我一个错误。 我经常得到”Unexpected Syntax Error &” or “Unexpected Syntax Error {” 我也尝试过使用jquery并在控制器中使用respond_to,但这似乎也不起作用。 我的想法是让json访问视图应该不是一个大问题,不应该需要jQuery,目前,我的页面源代码如下: var stuff = [{"node":{"ID":1301499692582,"Lat":42.3605063113369,"Lon":-71.0870862191138}},{"node":{"ID":1301499691515,"Lat":42.3605147089149,"Lon":-71.0870533282532}},{"node":{"ID":1301431075499,"Lat":42.3605456103,"Lon":-71.0875239075536}} etc 我不理解符号(可能是语法错误的来源),但是当我执行渲染时: json => @nodes.to_json ,页面呈现一个有效的普通json: [{“node”:{“ID”:1301499692582,”Lat”:42.3605063113369,”Lon”:-71.0870862191138}},{“node”:{“ID”:1301499691515,”Lat”:42.3605147089149,”Lon”:-71.0870533282532}},{“node”:{“ID”:1301431075499,”Lat”:42.3605456103,”Lon”:-71.0875239075536}} 注意:我也尝试过var stuff = ‘<%= @json.to_json %>但是当我执行var json […]

Ruby:PUT请求与JSON正文?

我需要使用ruby创建HTTP PUT请求 。 该请求具有JSON正文 我能够使用以下方法生成JSON主体: require ‘rubygems’ require ‘json’ jsonbody = JSON.generate[“message”=>”test”,”user”=>”user1″] 我需要将此PUT请求发送到url: require ‘open-uri’ url = URI.parse(‘http://www.data.com?access_token=123’) 有人可以告诉我如何在Ruby中做到这一点?

使用net / http发送Post请求

我需要将JSON中的数据发送到在同一台计算机上运行的另一个应用程序。 我这样发送请求(rails 3.2.13) data = { //some data hash } url = URI.parse(‘http://localhost:6379/api/plans’) resp, data = Net::HTTP.post_form(url, data.to_JSON ) p resp p data { resp: resp, data: data.to_JSON } 但我得到Net::HTTPBadResponse (wrong status line: “-ERR unknown command ‘POST'”):我该如何解决这个问题? 更新1 更新了我的代码@ Raja-d建议 url = URI.parse(‘http://localhost:6379/v1/sessions’) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true resp, data = Net::HTTP.post_form(url, data) […]

Ruby on Rails和来自URL的JSON解析器

我使用’gem json’并且需要从某个url加载JSON数据,例如: “http://locallhost:3000/qwerty/give_json.json” {“one”:”Omg”,”two”:125,”three”:”Hu”} 我有rails app class QwertyController < ApplicationController require 'json' def get_json source = "http://localhost:3000/qwerty/give_json.json" @data = JSON.parse(JSON.load(source)) end end 我收到错误 JSON::ParserError in QwertyController#get_json 795: unexpected token at ‘http://localhost:3000/qwerty/give_json.json’ 在字符串中:@data = JSON.parse(JSON.load(source)) 有什么事? 如何获取JSON数据并解析它? 我试试@data [“one”] ……

从x日期到结束日期,从几天开始迭代

我需要从2013年1月1日开始,并为每个日期“做一些事情”,从而为每个日期生成一个JSON文件。 我已经为一个日期制定了“做一些事情”部分,但是我很难从一个约会开始并循环到另一个结束日期。

will_paginate JSON支持?

我想知道是否有人可以告诉我,如果will_paginate可以支持开箱即用的JSON,或者是否必须被黑客入侵? 我想将页面数据添加到JSON响应中,而will_paginate管理分页。

使用curl将JSON数据POST到简单的rails应用程序

我使用模型入口设置了一个简单的新rails应用程序,使用scaffolding属性title和content 。 现在我正在尝试使用curl来发布JSON数据(而不是使用浏览器)。 以下似乎工作(即成功发布空数据): curl –verbose –header “Accept: application/json” –header “Content-type: application/json” –request POST –data “” http://localhost:3000/entries 以下不起作用: curl –verbose –header “Accept: application/json” –header “Content-type: application/json” –request POST –data “{‘content’:’I belong to AAA’,’title’:’AAA’}” http://localhost:3000/entries 我尝试了很多变化。 我得到的错误主要是JSON数据中未找到主机或意外令牌。

如果json有多个数据集,我该如何编写json模式

我是这个json架构的新手,如果它只有一个如下所示的数据集,我可以编写json架构 { “employees”: [ { “id”: 1, “name”: “aaa” } } 示例json-schema用于此 { “type” : “object”, “required” : [“employees”], “properties” : { “employees” : { “type” : “Array”, “items” : [ “properties” : { “id” : {“type” : “integer”}, “name” : {“type” : “string”}, }, “required” : [“id”,”name”] ] } } } 但是如果我们有多个数据集,我就会坚持在ruby中编写json模式 { “employees”: […]