I am new to jquery and JSON and this is what I am trying to do. I have a firstName field, lastName field and the list of date fields with class "actionDueDate_input". I want to generate a JSON, which has firstName, lastName and iterating through all the date fields and inserting them into JSON.
I am trying to do the following and it is giving me "undefined". There is something basic that is going wrong here, which I am not being able to figure out.
var dateval="";
$('div.action input.actionDueDate_input').each(function(index)
{
if(index>0)
{
dateval+=",";
}
dateval+="{";
dateval+='"';
dateval+="actionDueDate";
dateval+='"'+":";
dateval+='"'+$(this).val()+'"'+"}";
});
alert(dateval);
var values={"contact":
[
{
"givenName":givenName,
"familyName":familyName,
"actionSet":
[
dateval
],
}
]};
alert('Action Dates for client one: '+values.contact[0].actionSet[0].actionDueDate);
JSON takes the form of a specially formatted string, not an array. An array is what you get when you parse JSON.
To JSONify an array, use JSON.stringify(arr)
.
Here's an example that takes all the values of a primitive form and bundles them into a JSON string:
var formInputs = {},
jsonString;
$('form').find('input', 'select').each(function(){
formInputs[this.name] = this.value;
});
jsonString = JSON.stringify(formInputs);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.