从 javascript/jquery 到 ruby​​/sinatra 的多维数组

发布于 2024-08-10 07:24:58 字数 902 浏览 4 评论 0原文

请问如何将二维数组从 javascript 传递到 ruby​​?我在客户端有这个:

function send_data() {
    var testdata = {
        "1": {
            "name": "client_1",
            "note": "bigboy"
        },
        "2": {
            "name": "client_2",
            "note": "smallboy"
        }
    }

    console.log(testdata);
    $.ajax({
      type: 'POST',
      url: 'test',
      dataType: 'json',
      data: testdata
    });
  }

在服务器端有这个:

post '/test' do p params end

但我无法正确处理。我在服务器端能得到的最好的结果就是

{"1"=>"[object Object]", "2"=>"[object Object]"}

我尝试在客户端添加 JSON.stringify 并在服务器端添加 JSON.parse ,但第一个结果是

{"{\"1\":{\"name\":\"client_1\",\"note\":\"bigboy\"},\"2\":{\"name\":\"client_2\",\"note\":\"smallboy\"}}"=>nil}

后者抛出 TypeError - 无法将哈希转换为字符串。

任何人都可以帮忙,或者发布一小段正确的代码吗?谢谢

how do I pass a 2-dimensional array from javascript to ruby, please? I have this on client side:

function send_data() {
    var testdata = {
        "1": {
            "name": "client_1",
            "note": "bigboy"
        },
        "2": {
            "name": "client_2",
            "note": "smallboy"
        }
    }

    console.log(testdata);
    $.ajax({
      type: 'POST',
      url: 'test',
      dataType: 'json',
      data: testdata
    });
  }

and this on server side:

post '/test' do p params end

but I can't get it right. The best I could get on server side is something like

{"1"=>"[object Object]", "2"=>"[object Object]"}

I tried to add JSON.stringify on client side and JSON.parse on server side, but the first resulted in

{"{\"1\":{\"name\":\"client_1\",\"note\":\"bigboy\"},\"2\":{\"name\":\"client_2\",\"note\":\"smallboy\"}}"=>nil}

while the latter has thrown a TypeError - can't convert Hash into String.

Could anyone help, or maybe post a short snippet of correct code, please? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被翻牌 2024-08-17 07:24:58

您可能需要在 javascript 端手动构建 JSON:

[[{'object':'name1'},{'object':'name2'}],[...],[...]]

这将构建一个包含对象的数组。

它可能看起来像这样:

testdata = [[{
        "1": {
            "name": "client_1",
            "note": "bigboy"
        }],
        [{"2": {
            "name": "client_2",
            "note": "smallboy"
        }]
    }]

我这里可能有一些东西,但这应该接近它的样子。

You may want to build up the JSON manually, on the javascript side:

[[{'object':'name1'},{'object':'name2'}],[...],[...]]

This will build an array of arrays with objects.

It may look like this:

testdata = [[{
        "1": {
            "name": "client_1",
            "note": "bigboy"
        }],
        [{"2": {
            "name": "client_2",
            "note": "smallboy"
        }]
    }]

I may have something off here, but this should be close to what it would look like.

明明#如月 2024-08-17 07:24:58

我不确定这是否有帮助,但我有两个想法:序列化字段和/或迭代数组。

我设法通过设置序列化必须存储子数组的字段来将json数组放入activerecord对象中:

class MyModel < ActiveRecord::Base
  serialize :tags
end

并使用迭代器来处理json数组:

f = File.read("myarrayof.json")
jarray = JSON.load(f)
jarray.each { |j| MyModel.create.from_json(j.to_json).save }

来回转换似乎有点麻烦,但我发现它是最重要的处理数组的明显方法。

I'm not sure if this will help but I've got two thoughts: serialize fields and/ or iterate the array.

I managed to get a json array into activerecord objects by setting serializing the fields which had to store sub-arrays:

class MyModel < ActiveRecord::Base
  serialize :tags
end

and using an iterator to process the json array:

f = File.read("myarrayof.json")
jarray = JSON.load(f)
jarray.each { |j| MyModel.create.from_json(j.to_json).save }

The conversion back-and-forth seems a bit cumbersome but I found it the most obvious way to handle the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文