0
我有一個包含多值郵件屬性下面的JavaScript對象:構建多值JSON爲Rails
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
}
}
如果鍵入這在靜態,男y Rails應用程序正確處理它。但讓我知道是否有更好的方法是在服務器端處理它,以便我不必手動構建JSON。謝謝!
在我的具體使用情況下,我只好到最後PARAM改爲'processedContact.params [「接觸[emails_attributes] [」 + index +'] [account]'] = contact.email [label] .toString();'。非常感謝! –