为 Rails 构建多值 JSON
我有以下包含多值电子邮件属性的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我迭代
contact.email
时,我得到了标签。获取帐户只需返回原始联系人哈希即可,因此: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: