2016-09-19 6 views
10

मैं जब JSON पार्स करने का प्रयास एक त्रुटि हो रही है:जावास्क्रिप्ट - पार्सिंग JSON रिटर्न सिंटेक्स त्रुटि

SyntaxError: Unexpected token u in JSON at position 0(…) eFormsAtoZIndex.aspx:6558 

पूर्ण कोड: http://pastebin.com/LXpJN8GF

प्रासंगिक कोड:

$(document).ready(function() { 
    var rebuild = getParameterByName("rebuild"); 
    var createdStructures = $('#AtoZContentDiv').children().length; 
    if ((rebuild !== undefined && rebuild !== null && rebuild.indexOf("true") === 0) || (createdStructures === 0)) { 
     // clean up pre-existing data 
     cleanUp(); 

     // create container structure 
     createFormLinkContainers(); 

     // Call SP web services to retrieve the information and create the A to Z 
     retrieveListData(); 
     completeInitialization(); 
    } else { 
     try { 
      aggregateAll = jQuery.parseJSON($('#hdnAggregateAll').val()); 
      console.log(jQuery.parseJSON($('#hdnAggregateAll').val())); 
      aggregatePersonal = jQuery.parseJSON($('#hdnAggregatePersonal').val()); 
      aggregateBusiness = jQuery.parseJSON($('#hdnAggregateBusiness').val()); 
      ministryAggregate = jQuery.parseJSON($('#hdnMinistryAggregate').val()); 
      caAggregate = jQuery.parseJSON($('#hdnCAAggregate').val()); 
      sTaxAggregate = jQuery.parseJSON($('#hdnSTaxAggregate').val()); 
      bTaxAggregate = jQuery.parseJSON($('#hdnBTaxAggregate').val()); 
      leTaxAggregate = jQuery.parseJSON($('#hdnLETaxAggregate').val()); 
     } catch (err) { 
      console.log(err); 
     } 

     var type = getParameterByName("filter"); 
    } 
    $("#tab-all").click(function() { 
     loadit('all'); 
    }); 

    $("#tab-business").click(function() { 
     loadit('business'); 
    }); 

    $(document).on('click', '#tab-personal', function(e) { 
     loadit('personal'); 
    }); 

    buildFilterMenu(); 
    loadit('all'); 

}); 

function createJSONStructure(title, desc, index, type, formLink, documentLink, pubType, processId, ministry, ca, stax, btax, letax) { 
    if (desc !== undefined && desc !== null) { 
     desc = desc.replace(/&lt;/g, "<").replace(/&gt;/g, ">"); 
    } else { 
     desc = ""; 
    } 
    var typeArr = []; 
    type = type.replace(/&amp;/, "&"); 

    var tempType = type.split("&"); 

    for (i = 0; i < tempType.length; i++) { 
     typeArr.push(tempType[i].trim()); 
    } 

    if (formLink === undefined || formLink === null || formLink.length === 0) { 
     formLink = ""; 
    } 

    if (documentLink === undefined || documentLink === null || documentLink.length === 0) { 
     documentLink = ""; 
    } 

    // subject, business and life event taxonomies must cater for multiple entries 
    var staxStructure = buildTaxonomyJSONStructure(stax, "stax"); 
    var btaxStructure = buildTaxonomyJSONStructure(btax, "btax"); 
    var letaxStructure = buildTaxonomyJSONStructure(letax, "letax"); 

    var json = { 
     'name': title, 
     'desc': desc, 
     'type': typeArr, 
     'pubType': pubType, 
     'pdflink': documentLink.split(",")[0].replace(/\'/g, "&#39;"), 
     'formlink': formLink.split(",")[0].replace(/\'/g, "&#39;"), 
     'processid': processId, 
     'index': index, 
     'ministry': ministry.replace(/\,/g, " "), 
     'ca': ca.replace(/\,/g, " "), 
     'stax': staxStructure, 
     'btax': btaxStructure, 
     'letax': letaxStructure 
    }; 
    return json; 
} 



function completeInitialization() { 
    if (checkDataLoaded()) { 
     // add the Navigation to the containers once all the data is inserted 
     addNavigationToContainers(); 


     var type = getParameterByName("filter"); 
     if (type == null || type.length == 0) { 
      type = "all"; 
     } 

     loadit(type); 

     buildFilterMenu(); 

     filter(type); 

     $('#hdnAggregateAll').val(stringify(aggregateAll)); 
     console.log(aggregateAll); 
     $('#hdnAggregatePersonal').val(stringify(aggregatePersonal)); 
     $('#hdnAggregateBusiness').val(stringify(aggregateBusiness)); 
     $('#hdnMinistryAggregate').val(stringify(ministryAggregate)); 
     $('#hdnCAAggregate').val(stringify(caAggregate)); 
     $('#hdnSTaxAggregate').val(stringify(sTaxAggregate)); 
     $('#hdnBTaxAggregate').val(stringify(bTaxAggregate)); 
     $('#hdnLETaxAggregate').val(stringify(leTaxAggregate)); 
    } else { 
     retryCount += 1; 

     // Check that the maximum retries have not been exceeded 
     if (retryCount <= maxRetries) { 
      setTimeout("completeInitialization();", 1000 * retryCount); 
     } 
    } 
} 

किसी का कहना है सकते हैं जेएसओएन संरचना या जेएस के साथ क्या गलत है या मैं इसके अंदर वस्तुओं को कैसे डिबग कर सकता हूं?

संपादित करें (Jaromanda एक्स के और सीएच बकिंघम जबाब के अनुसार):

$('#hdnAggregateAll').val(JSON.stringify(aggregateAll)); 
console.log(aggregateAll);   $('#hdnAggregatePersonal').val(JSON.stringify(aggregatePersonal));   $('#hdnAggregateBusiness').val(JSON.stringify(aggregateBusiness));   $('#hdnMinistryAggregate').val(JSON.stringify(ministryAggregate)); 
$('#hdnCAAggregate').val(JSON.stringify(caAggregate)); 
$('#hdnSTaxAggregate').val(JSON.stringify(sTaxAggregate)); 
$('#hdnBTaxAggregate').val(JSON.stringify(bTaxAggregate)); 
$('#hdnLETaxAggregate').val(JSON.stringify(leTaxAggregate)); 

त्रुटि:

10:42:24.274 TypeError: item is undefined 
createFormLinks/<()eformsAtoZIndex.aspx:5644 
.each()jquery-1.11.1.min.js:2 
createFormLinks()eformsAtoZIndex.aspx:5638 
processResult()eformsAtoZIndex.aspx:5507 
m.Callbacks/j()jquery-1.11.1.min.js:2 
m.Callbacks/k.fireWith()jquery-1.11.1.min.js:2 
x()jquery-1.11.1.min.js:4 
.send/b()jquery-1.11.1.min.js:4 
1eformsAtoZIndex.aspx:5644:1 

लाइन पर:

if (item.processid !== "0") 

ब्लॉक में:

function createFormLinks(formItems, index) 
    { 
     // create all links on the page and add them to the AtoZContent div for now 
     var parentContainer = $("#AtoZContentDiv"); 

     if (parentContainer === null) 
     { 
      // if it doesn't exist, we exist cause I can't reliably add a new control to the body and get the display 
      // location correct 
      return; 
     } 

     // sort form link array first 
     formItems = sortResults(formItems, 'name', true); 

     var count = 0; 

     $.each(formItems, function(i, item) 
     { 
      var link; 
      count = count + 1; 

      //add links to parent container 
      if (item.processid !== "0") 
      { 
       link = item.formlink; 
      } 
      else if (item.pdflink !== "") 
      { 
       link = item.pdflink; 
      } 

      var container = $("#AtoZContent-" + index); 
      var itemType = "all"; 

      if (item.type !== null && item.type !== undefined && item.type.length === 1) itemType = item.type[0]; 



      var str = "<div id='divFormLink-" + index + "-" + count + "' type='" + itemType + "' "; 

     if (item.name !== undefined && item.name !== null) 
      { 
       str = str + " ministry='" + stripPunctuation(item.ministry) + "' "; 
       str = str + " ca='" + stripPunctuation(item.ca) + "' "; 

       // now, we need to handle these differently since they can have multiple values 
       str = str + " stax='"; 
       for (i = 0; i < item.stax.length; i++) 
       { 
        str = str + stripPunctuation(item.stax[i]); 
       } 
       str = str + "' "; 

       str = str + " btax='"; 
       for(i = 0; i < item.btax.length; i++) 
       { 
        str = str + stripPunctuation(item.btax[i]); 
       } 
       str = str + "' "; 

       str = str + " letax='"; 
       for(i = 0; i < item.letax.length; i++) 
       { 
        str = str + stripPunctuation(item.letax[i]); 
       } 
       str = str + "' "; 
      } 

      str = str + " index='" + index + "' style='word-wrap: break-word;'></div>"; 
     container.append(str); 

      var innerDiv = $("#divFormLink-" + index + "-" + count); 
      appendIcon(innerDiv, item.pubType); 
      innerDiv.append("<a id='formLink-" + index + "-" + count + "' href='" + link + "'>" + item.name + "</a>"); 
      innerDiv.append("<div id='formDesc-" + index + "-" + count + "'>" + item.desc + "</div><br />"); 

     }); 
    } 
+3

, अगर शून्य, या एक संख्या है, या एक बूलियन, या एक तिथि, या कुछ भी है कि नहीं है एक वस्तु अपने stringify कार्य करने के लिए पारित कर दिया है, यह 'रिटर्न undefined' - जो जब करने के लिए मजबूर एक स्ट्रिंग आपको उस सटीक त्रुटि देगा जब आप इसे पार्स करने का प्रयास करेंगे। यदि आप JSON स्ट्रिंग को वापस करने की अपेक्षा करते हैं तो आप अपने स्ट्रिंगफ़ीफ़ फ़ंक्शन को जटिल क्यों कर रहे हैं? बस JSON.stringify का उपयोग करें और इसके साथ किया जाए –

+0

जारोमांडा एक्स के साथ सहमत हों, अपनी कस्टम स्ट्रिंग को हटाएं, JSON.stringify का उपयोग करें। देखें कि आपकी समस्या दूर हो गई है या नहीं। –

+0

@CHBuckingham मुझे एक प्रकार की त्रुटि मिलती है: 'TypeError: आइटम अपरिभाषित है'। मेरे द्वारा किए गए परिवर्तनों के लिए अद्यतन प्रश्न देखें। – Brian

उत्तर

10

लाइन 155 पर आप json दबाएंगे, भले ही यह अपरिभाषित हो।

if (pubType=="eForm" || pubType=="PDF") { 
    var json = createJSONStructure(title, desc, index, type.toLowerCase(), formLink, documentLink, pubType, processId, ministry, ca, stax, btax, letax); 
} 
formItems.push(json); 

और यह करने के बाद अपने अपरिभाषित की item.processid प्राप्त करने की कोशिश कर रहे हैं। यदि आप if-block में चर परिभाषित कर सकते हैं, लेकिन मामले में आपको कुछ सत्यापन जोड़ना चाहिए।

$.each(formItems, function(i, item) { 
    var link; 
    count = count + 1; 

    if (item == null) { 
     return; 
    } 

    //add links to parent container 
    if (item.processid !== "0") 
    ... 
}); 
एक बात के लिए
+0

मुझे नहीं पता कि ब्रायन क्या बिल्ली कर रहा है, या उसने इस जवाब को सही क्यों नहीं चिह्नित किया है, उस समय यह एक दबदबात्मक मामला था। यह स्पष्ट रूप से (केवल) सही उत्तर है। – TylerY86

संबंधित मुद्दे