Reputation: 23
I am getting below error when i serialize json. Unterminated string passed in. This is the code i have.
function updatemetaData(docid) {
$(document).ready(function(){
var list = new Array();
var strOrderArr = '';
$('input[id=varchar]').each(function (i, item) {
list.push($(item).val());
strOrderArr = strOrderArr + "{";
strOrderArr = strOrderArr + "'upld_id':" + "'" + docid + "'";
strOrderArr = strOrderArr + "'upld_contentvalue':" + "'" + $(item).val() + "'";
strOrderArr = strOrderArr + "},";
});
var jsonOfLog = JSON.stringify(strOrderArr);
$.ajax({
type: 'POST',
data: "jsonOfLog=" + jsonOfLog,
dataType: 'json',
url: '/documentVerification/updatedocDetails',
success: function (data) {
fun_toastr_notify('success', 'Document has been Updated');
$("#dialog").dialog("close");
}
, error: function (error) {
fun_toastr_notify('error', data, 'Error');
}
});
});
}
listInuploadContent = (CommonUtility.JsonDeserialize<List<tr_upld_content>>("[" + jsonOfLog.Substring(0, jsonOfLog.Length - 1) + "]")) as List<tr_upld_content>;
public static T JsonDeserialize<T>(string json)
{
try
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<T>(json);
}
catch (Exception ex)
{
throw ex;
}
}
Below is the output with errors.
Unterminated string passed in. (146): ["{'upld_id':'2185''upld_contentvalue':'123'},{'upld_id':'2185''upld_contentvalue':'31/08/2016'},{'upld_id':'2185''upld_contentvalue':'Karkala'},]
Upvotes: 1
Views: 5213
Reputation: 11502
Try with below code:
$('input[id=varchar]').each(function (i, item) {
list.push($(item).val());
strOrderArr = strOrderArr + "{";
strOrderArr = strOrderArr + "'upld_id':" + "'" + docid + "'";
strOrderArr = strOrderArr + "'upld_contentvalue':" + "'" + $(item).val() + "'";
strOrderArr = strOrderArr + "},";
});
strOrderArr = strOrderArr.replace(/\//g, "\\/");
var jsonOfLog = JSON.stringify(strOrderArr);
I have added a ,
after docid
value to separate two key-value pair.
Upvotes: 1
Reputation: 21836
Use the JavaScript serializer to serialize to JSON. You are missing a comma in manual serialization.
strOrderArr = strOrderArr + "'upld_id':" + "'" + docid + "', ";
strOrderArr = strOrderArr + "'upld_contentvalue':" + "'" + $(item).val() + "'";
I have added a comma after docid in the above code.
Upvotes: 0