1

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'},]

SHAJAHAN KP
  • 23
  • 1
  • 4

2 Answers2

1

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.

vijayP
  • 11,432
  • 5
  • 25
  • 40
  • I tried with above code. I am getting below error {"Unterminated string passed in. (149): [\"{'upld_id':'2185','upld_contentvalue':'123'},{'upld_id':'2185','upld_contentvalue':'31/08/2016'},{'upld_id':'2185','upld_contentvalue':'Karkala'},]"} – SHAJAHAN KP Aug 25 '16 at 05:15
  • i tried as you updated now i am getting error as below Unterminated string passed in. (150): ["{'upld_id':'2185''upld_contentvalue':'123'},{'upld_id':'2185''upld_contentvalue':'31\\/08\\/2016'},{'upld_id':'2185''upld_contentvalue':'Karkala'},] – SHAJAHAN KP Aug 25 '16 at 05:30
  • Hi vijay you there? – SHAJAHAN KP Aug 25 '16 at 05:44
  • hello. could you please try sending JSON object directly without stringify it. E.g. http://stackoverflow.com/questions/8517071/send-json-data-via-post-ajax-and-receive-json-response-from-controller-mvc – vijayP Aug 25 '16 at 05:48
0

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.

vijayst
  • 20,359
  • 18
  • 69
  • 113