Tag: json

WebMock:Rspec – 使用JSON响应测试Facebookvalidation

我很难为我的程序编写规范,因为我需要在填写所有正确的格式信息后validationfacebook ID。 我的facebook ID是从JSON响应中检索的,所以我不知道如何在我的规范中描述它。 我对这个领域很陌生,而且我已经被困在这一点很长一段时间了。 如果有一些示例代码可以帮助我,我将不胜感激。 以下是我的半成品规格。 场景:如果用户名和fbid相同,则使用用户给定的Fbid通过使用“ http://graph.facebook.com ”(JSON响应)来validationFacebook页面。 我的githuburl: https : //github.com/amycheong/company_list 更新:我使用过WebMock但最终获得: 1) Companies new company create with valid information correct facebook id should validate fbid Failure/Error: Company.validate_fbid(‘pepsi’)[‘username’].should == “pepsi” WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: GET http://graph.facebook.com:443/pepsi with headers {‘Accept’=>’*/*’, ‘User-Agent’=>’Ruby’} You can stub this request with the following […]

错误无法在Windows 7×64上构建json gem native扩展

我正在尝试安装devkit然后在Windows7 64bits上安装json gem,我已经安装了ruby 4.2.4并解压缩了devkit配置的config.yml并运行ruby dk.rb install然后当我运行gem install JSON时–platform = ruby但它给出了如下错误: current directory: C:/Ruby22/lib/ruby/gems/2.2.0/gems/json-1.8.3/ext/json/ext/generator C:/Ruby22/bin/ruby.exe -r ./siteconf20160205-1668-13fh07v.rb extconf.rb creating Makefile current directory: C:/Ruby22/lib/ruby/gems/2.2.0/gems/json-1.8.3/ext/json/ext/generator make “DESTDIR=” clean current directory: C:/Ruby22/lib/ruby/gems/2.2.0/gems/json-1.8.3/ext/json/ext/generator make “DESTDIR=” generating generator-i386-mingw32.def compiling generator.c In file included from c:/Ruby22/include/ruby-2.2.0/ruby.h:33:0, from ../fbuffer/fbuffer.h:5, from generator.c:1: c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:111:14: error: size of array ‘ruby_check_sizeof_voidp’ is negative In file included from […]

如何在.to_json方法上获取自定义属性?

例如,我有这个模型: class User < ActiveRecord::Base def foo bar end end 当我这样做时: 我明白了: [{“user”:{“created_at”:“2011-06-07T17:40:21-03:00”,“login”:“abcd”,“password”:“1234”,“updated_at”:“2011- 06-07T18:10:04-03:00″ }}] 我怎么能在这个json上得到foo? 另外,foo也是一个活跃的记录.. 谢谢

如何在sqlite中存储json数据

我很难将数据作为’JSON’格式存储到我的sqlite数据库中以用于rails应用程序。 我已经搜索了如何在我的sqlite数据库中将数据存储为JSON,但我没有看到许多有前景的替代方案。 谁可以指导我如何做到这一点?

如何关闭仅用于json的rails protect_from_forgeryfilter

我有使用Rails3构建的网站,现在我想为移动客户端访问实现json API。 但是,由于protect_from_forgeryfilter,从客户端发送json post请求。 因为客户端不会从服务器检索任何数据,所以客户端无法接收auth_token,因此我只想为json请求关闭protect_from_forgery选项(我认为rails3在默认情况下执行此操作,但显然它没有) 。 我知道在这里讨论类似的主题,但在这种情况下,他在发送post请求之前收到auth_token。 所以我的问题是只关闭json的protect_from_forgery是这样做的好方法吗? 如果是,怎么办? 如果不是,还有什么选择? 仅供参考,我使用以下ajax请求 $.ajax({ type: ‘POST’, url: “http://www.example.com/login.json”, data: { ’email’: emailVal, ‘password’: passwordVal }, success: onSuccess, error: onError, dataType: “json” }); 我得到ActionController :: InvalidAuthenticityToken错误。 顺便说一句,遵循curl命令工作虽然… curl -H“Content-Type:application / json”-c cookies.txt -d'{“email”:emailVal,“password”:passwordVal}’-X POST http://www.example.com/login.json -一世

JSON包括条件

我的设置:Rails 2.3.10,Ruby 1.8.7 假设我有这段代码片段 user = User.find(1) user.to_json(:include => :posts) 如果我想将用户的post包含在某个条件中,例如只有一周的post,该怎么办?

Rails 3:转义字符(\)出现在JSON字符串的一部分中

任何人都知道为什么我的一些json元素被反斜杠( \ )转义而其他人没有? {“first”:”John”,”last”:”Smith”,”dogs”:”[{\”name\”:\”Rex\”,\”breed\”:\”Lab\”},{\”name\”:\”Spot\”,\”breed\”:\”Dalmation\”},{\”name\”:\”Fido\”,\”breed\”:\”Terrier\”}]”} 理想情况下,我希望他们中没有人逃脱…… 这是通过在两个模型中重写as_json生成的。 人has_many狗。 #models/person.rb class Person first, :last => last, :dogs => dogs.to_json } end end #models/dog.rb class Dog name, :breed => breed } end end

to_json在字段中返回nil,如果它的黑色,ios不能解释ror中的null

如何在to_json的输出中将字段值从null更改为“”? 它目前返回 {“name”:”priya”,”mobile”:null} 相反,我想要 {“name”:”priya”,”mobile”:””} 要么 {“name”:”priya”,”mobile”:”NA”} 请建议

如何扫描单行参数字符串并返回两个数组?

我有以下单行字符串: params = {“FieldStructure”=>”{\”Fields\”:[{\”Title\”:\”Company Name\”,\”Type\”:\”text\”,\”ID\”:\”Field6\”},{\”Title\”:\”Email\”,\”Type\”:\”email\”,\”ID\”:\”Field5\”}]}”, “Field6″=>”tettetette”, “Field5″=>”test@tessss.pl”, “EntryId”=>”25”} 格式版: params = { “FieldStructure”=>” {\”Fields\”:[ {\”Title\”:\”Company Name\”,\”Type\”:\”text\”,\”ID\”:\”Field6\”}, {\”Title\”:\”Email\”,\”Type\”:\”email\”,\”ID\”:\”Field5\”}]}”, “Field6″=>”tettetette”, “Field5″=>”test@tessss.pl”, “EntryId”=>”25” } 我想收到: [[“Company Name”, “Field6”], [“Email”, “Field5″]] 我尝试使用扫描方法,但有一些问题: >> fields = params[‘FieldStructure’].scan(/Title\”:\”(.+?)\”.+ID\”:\”(.+?)\”/) => [[“Company Name”, “Field5”]] 怎么做到这一点?

如何使用post方法在ROR的def create函数中从客户端获取数据

我正在努力解决这些问题,如何从客户端获取数据,如何在def create function中使用post方法,从客户端获取数据。 客户端编码是android,服务器端代码是ROR。 服务器端编码在ror def create respond_to do |format| @post = Post.new(params[:post]) if @post.save flash[:notice] = “Prayer Successfully created.” @posts = Post.paginate(page: params[:page],:per_page => 5) format.json{ render :json => @post, :status => :created } else flash[:notice] = “Error” @posts = Post.paginate(page: params[:page],:per_page => 5) format.json{ render :json => @post, :status => :created } end […]