Javascript - 解析JSON会返回语法错误

时间:2016-09-19 12:52:26

标签: javascript jquery 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);
        }
    }
}

有人能指出JSON结构或JS有什么问题,或者我如何调试其中的项目?

编辑(按照Jaromanda X&CH和Buckingham的回复):

$('#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));

ERROR:

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")

在Block:

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 />");

        });
    }

1 个答案:

答案 0 :(得分:10)

在第155行,即使它未定义,也会推送# -*- coding: mbcs -*- # Do not delete the following import lines from abaqus import * from abaqusConstants import * import __main__ def OdbMacro1(): import section import regionToolset import displayGroupMdbToolset as dgm import part import material import assembly import step import interaction import load import mesh import optimization import job import sketch import visualization import xyPlot import displayGroupOdbToolset as dgo import connectorBehavior import os os.chdir(r"C:\FolderPath") session.mdbData.summary() o1 = session.openOdb(name='C:\FolderPath\odb.odb') session.viewports['Viewport: 1'].setValues(displayedObject=o1) odb = session.odbs['C:\FolderPath\odb.odb'] xy0 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10 in NSET PADSURF_BACK', steps=('Step-2', ), suppressQuery=True) xy1 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10053 in NSET PADSURF_BACK', steps=('Step-2', ), suppressQuery=True) xy2 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10054 in NSET PADSURF_BACK', steps=('Step-2', ), suppressQuery=True) xy3 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10055 in NSET PADSURF_BACK', steps=('Step-2', ), suppressQuery=True) xy4 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='Nodal temperature: NT11 PI: PAD-1 Node 10056 in NSET PADSURF_BACK', steps=('Step-2', ), suppressQuery=True) xy5 = avg((xy0, xy1, xy2, xy3, xy4, ), ) session.XYData(name='x0.nt11', objectToCopy=xy5, sourceDescription='avg((Nodal temperature: NT11 PI: PAD-1 Node 10 in NSET PADSURF_BACK, Nodal temperature: NT11 PI: PAD-1 Node 10053 in NSET PADSURF_BACK, Nodal temperature: NT11 PI: PAD-1 Node 10054 in NSET PADSURF_BACK, Nodal temperature: NT11 PI: PAD-1 Node 10055 in NSET PADSURF_BACK, Nodal temperature: NT11 PI: PAD-1 Node 10056 in NSET PADSURF_BACK, ),)') odb = session.odbs['C:\FolderPath\odb.odb'] xy0 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='RFLE11: RFLE11 PI: PAD-1 Node 1', steps=('Step-2', ), suppressQuery=True) xy1 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='RFLE11: RFLE11 PI: PAD-1 Node 2', steps=('Step-2', ), suppressQuery=True) xy2 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='RFLE11: RFLE11 PI: PAD-1 Node 3', steps=('Step-2', ), suppressQuery=True) [...] xy280068 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='RFLE11: RFLE11 PI: SLIDER-1 Node 210034', steps=( 'Step-2', ), suppressQuery=True) xy280069 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='RFLE11: RFLE11 PI: SLIDER-1 Node 210035', steps=( 'Step-2', ), suppressQuery=True) xy280070 = sum((xy0, xy1, xy2, xy3, xy4, xy5, xy6, xy7, xy8, xy9, xy10, xy11, xy12, xy13, xy14, xy15, xy16, xy17, xy18, xy19, xy20, xy21, xy22, xy23, xy24, xy25, xy26, xy27, xy28, xy29, xy30, xy31, xy32, xy33, xy34, xy35, [...] xy280057, xy280058, xy280059, xy280060, xy280061, xy280062, xy280063, xy280064, xy280065, xy280066, xy280067, xy280068, xy280069, ), ) session.XYData(name='model.RFLE', objectToCopy=xy280070, sourceDescription='sum((RFLE11: RFLE11 PI: PAD-1 Node 1, RFLE11: RFLE11 PI: PAD-1 Node 2, RFLE11: RFLE11 PI: PAD-1 Node 3, [...] RFLE11: RFLE11 PI: SLIDER-1 Node 210033, RFLE11: RFLE11 PI: SLIDER-1 Node 210034, RFLE11: RFLE11 PI: SLIDER-1 Node 210035, ),)') odb = session.odbs['C:\FolderPath\odb.odb'] xy0 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 5', steps=('Step-2', ), suppressQuery=True) xy1 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 6', steps=('Step-2', ), suppressQuery=True) xy2 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12 [................................................................] =True) xy6000 = xyPlot.XYDataFromHistory(odb=odb, outputVariableName='Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18048', steps=('Step-2', ), suppressQuery=True) xy6001 = sum((xy0, xy1, xy2, xy3, xy4, xy5, xy6, xy7, xy8, xy9, xy10, xy11, xy12, xy13, xy14, xy15, xy16, xy17, xy18, xy19, xy20, xy21, xy22, xy23, [................................................................] xy5991, xy5992, xy5993, xy5994, xy5995, xy5996, xy5997, xy5998, xy5999, xy6000, ), ) session.XYData(name='surf.hfla', objectToCopy=xy6001, sourceDescription='sum((Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 5, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 6, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12050, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12051, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12052, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12053, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12054, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12055, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12056, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12057, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12058, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12059, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12060, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12061, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 12062, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ [................................................................] 37, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18038, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18039, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18040, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18041, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18042, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18043, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18044, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18045, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18046, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18047, Heat flux: HFLA ASSEMBLY_SLIDERSURF/ASSEMBLY_PADSURF PI: SLIDER-1 Node 18048, ),)') x0 = session.xyDataObjects['surf.hfla'] x1 = session.xyDataObjects['model.RFLE'] x2 = session.xyDataObjects['x0.nt11'] session.xyReportOptions.setValues(interpolation=ON) session.writeXYReport(fileName='C:\FolderPath\report.rpt', appendMode=OFF, xyData=(x0, x1, x2)) OdbMacro1()

Traceback (most recent call last):
  File "macro.py", line 3, in <module>
    from abaqus import *
  File "SMAPyaModules\SMAPyaAbqPy.m\src\abaqus.py", line 15, in <module>
ImportError: abaqus module may only be imported in the Abaqus kernel process

在此之后,你试图得到json未定义的。 您可以在if-block中定义变量,但在这种情况下您应该添加一些验证。

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);