为 Rails 构建多值 JSON

发布于 2024-11-19 17:59:18 字数 1614 浏览 4 评论 0原文

我有以下包含多值电子邮件属性的 javascript 对象:

var contact = {
    email =     {
        home =         (
            "[email protected]"
        );
        work =         (
            "[email protected]"
        );
    };
    emailCount = 2;
    firstName = Micah;
    lastName = Alcorn;
}

我需要构造以下 JSON 以发送到 Rails 服务器:

processedContact.params = {
    'contact[first_name]':'Micah',
    'contact[last_name]':'Alcorn',
    'contact[emails_attributes][0][label]':'home',
    'contact[emails_attributes][0][account]':'[email protected]',
    'contact[emails_attributes][1][label]':'work',
    'contact[emails_attributes][1][account]':'[email protected]',
};

我不知道如何克服以下内容:

function processContact(contact) {
    processedContact = {};
    processedContact.params = {
        'contact[first_name]':contact.firstName,
        'contact[last_name]':contact.lastName,
        // ????????
    };
    for (each in contact.email) {
        // this can be used to produce the email.account values, but not the email.labels
    }
}

如果我静态输入此内容,我的 Rails应用程序正确处理它。但请告诉我是否有更好的方法在服务器端处理它,这样我就不必手动构建 JSON。谢谢!

I have the following javascript object containing a multi-value email property:

var contact = {
    email =     {
        home =         (
            "[email protected]"
        );
        work =         (
            "[email protected]"
        );
    };
    emailCount = 2;
    firstName = Micah;
    lastName = Alcorn;
}

And I need to construct the following JSON to send to a Rails server:

processedContact.params = {
    'contact[first_name]':'Micah',
    'contact[last_name]':'Alcorn',
    'contact[emails_attributes][0][label]':'home',
    'contact[emails_attributes][0][account]':'[email protected]',
    'contact[emails_attributes][1][label]':'work',
    'contact[emails_attributes][1][account]':'[email protected]',
};

I don't know how to get past the following:

function processContact(contact) {
    processedContact = {};
    processedContact.params = {
        'contact[first_name]':contact.firstName,
        'contact[last_name]':contact.lastName,
        // ????????
    };
    for (each in contact.email) {
        // this can be used to produce the email.account values, but not the email.labels
    }
}

If I type this in statically, my Rails app handles it correctly. But let me know if there is a better was to handle it on the server side so that I don't have to manually construct the JSON. Thanks!

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

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

发布评论

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

评论(1

中二柚 2024-11-26 17:59:19

当我迭代 contact.email 时,我得到了标签。获取帐户只需返回原始联系人哈希即可,因此:

function processContact(contact) {
    processedContact = {};
    processedContact.params = {
        'contact[first_name]':contact.firstName,
        'contact[last_name]':contact.lastName,
    };
    var index = 0;
    for (label in contact.email) {
        processedContact.params['contact[emails_attributes]['+index+'][label]'] = label;
        processedContact.params['contact[emails_attributes]['+index+'][account]'] = contact[label];
        index++;
    }
    return processedContact;
}

When I iterate through the contact.email I get labels. Getting the account is simply a matter of going back to the original contact Hash, thus:

function processContact(contact) {
    processedContact = {};
    processedContact.params = {
        'contact[first_name]':contact.firstName,
        'contact[last_name]':contact.lastName,
    };
    var index = 0;
    for (label in contact.email) {
        processedContact.params['contact[emails_attributes]['+index+'][label]'] = label;
        processedContact.params['contact[emails_attributes]['+index+'][account]'] = contact[label];
        index++;
    }
    return processedContact;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文