使用 Ruby/Rails 发布多个对象(JSON 格式错误?)
这更像是一个“我做这个正确的问题吗”。
基本上我已经完成了标准的客户端服务器设置。
我正在尝试将多个对象作为一个对象发布到服务器。发布的对象是:
1:一维字符串数组(非常简单)@headers
2:哈希数组,每个哈希数组包含大约 7 个值和键。 @contentsArray
我想像下面这样的东西就可以解决问题
@postedInfo = {:info =>
{
:headers =>@headers,
:content => @contentsArray
}
}
myJsonReq = @postedInfo
puts "ITS A MAAAAAAAAAAADDDDD HOUUUUUSSSSSEEEEE" #Sorry, I just saw rise of the planet of the apes
puts myJsonReq.as_json
res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3007/update.json'),myJsonReq)
定义的URL显然是服务器,但它的实现方式如下
format: json
action: update_repo
pages: "content Page Title ........."very long string with no brackets or resemblance of JSON" followed by the headers part
headers: all strings are in here
controller: Update
如果我创建一个像这样的变量
@x = (params[:pages])
and puts @x.class
@x是一个字符串,在其他代码位中它将通过作为一个对象。具有无关访问权限的数组或哈希。
当然,我认为我必须从 JSON 反序列化它,所以我使用了这
JSON.parse(@x)
JSON.parse(params)
两个行,这两个行都抛出 JSON::ParserError (745: Unexpected token at 'content .... 然后是字符串的其余部分。
我想我正在发送对象是正确的还是我构建它们的方式错误?
This is more of an "am i doing this right question".
Basically I have your standard client server setup going.
I'm trying to post multiple objects as one to the server. The objects being posted are:
1: A 1d array of strings (pretty straightforward) @headers
2: An array of hashes each of which contain about 7 values and keys. @contentsArray
I figures something like the following would do the trick
@postedInfo = {:info =>
{
:headers =>@headers,
:content => @contentsArray
}
}
myJsonReq = @postedInfo
puts "ITS A MAAAAAAAAAAADDDDD HOUUUUUSSSSSEEEEE" #Sorry, I just saw rise of the planet of the apes
puts myJsonReq.as_json
res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3007/update.json'),myJsonReq)
The URL defined is obviously the server but how it comes through is like the following
format: json
action: update_repo
pages: "content Page Title ........."very long string with no brackets or resemblance of JSON" followed by the headers part
headers: all strings are in here
controller: Update
If I create a variable like so
@x = (params[:pages])
and puts @x.class
@x is a string where in other bits of code It would come through as an object. Either array or hash with indifferent access.
Naturally I thought that I has to deserialize it from JSON so I used the line's
JSON.parse(@x)
JSON.parse(params)
both of which threw JSON::ParserError (745: unexpected token at 'content .... and then the rest of the string.
I think I'm sending the objects right or am I constructing them the wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自精细手册:
您正在传递符号到哈希值的哈希值。如果您深入研究源代码,您会发现它最终会经历以下过程:
如果
vs
是一个哈希(如您的情况),您将不会得到任何有用的信息。例如,将所有这些应用于{:a =>; {:b => :c}}
你会得到这个:这并不是很有用。
因此,如果您想使用
Net::HTTP.post_form
,那么您必须自己将数据扁平化为简单的一级哈希。From the fine manual:
You're passing in a Hash of Symbol to Hash. If you dig into the source, you'll see that it ends up going through this:
If the
vs
is a Hash (as in your case), you won't get anything useful coming through. For example, apply all that to{:a => {:b => :c}}
you'll get this:And that's not terribly useful.
So, if you want to use
Net::HTTP.post_form
, then you'll have to flatten your data into a simple one level Hash yourself.