检索Json对象值

时间:2016-09-12 05:47:32

标签: javascript jquery json extract jsplumb

我目前正致力于根据提供的Json输入创建模型,反之亦然。

问题 是当我尝试检索predefinedStreamnamekind等对象时,它显示未定义虽然前两个idclasses已在警报中正确显示。

保存的Json输出

saved Json

生成警报消息

alert

在提供Json时加载元素

当我将以前生成的代码复制到文本区域并单击加载时,我得到以下内容

ui

警告上述对象

alert

在Json对象中保存数据

function saveFlowchart() {
    var node = [];
    var matches = [];
    var totalElementCount = 0;
    var searchEles = document.getElementById("container").children;
    for (var i = 0; i < searchEles.length; i++) {
        matches.push(searchEles[i]);
        var idOfEl = searchEles[i].id;
        totalElementCount = idOfEl;

        if (searchEles[i].id != null || searchEles[i].id != "") {
            var $element = $("#" + searchEles[i].id);
            var dropElem = $("#" + searchEles[i].id).attr('class');

            var position = $element.position();

            var elId = parseInt(idOfEl);

            if (dropElem == "streamdrop ui-draggable") {
                node.push({
                    id: idOfEl,
                    class: dropElem,
                    position: {
                        top: position.top,
                        left: position.left,
                        bottom: position.bottom,
                        right: position.right
                    }
                });

                for (var count = 0; count < 100; count++) {
                    if (createdImportStreamArray[count][0] == idOfEl) {
                        node.push({
                            predefinedStream: createdImportStreamArray[count][1],
                            name: createdImportStreamArray[count][2],
                            kind: "import"
                        });
                    }
                }
            }
        }
    }
}

检索Json对象值以创建元素

function loadFlowchart(e) {
    var flowChartJson = $('#jsonOutput').val();
    var flowChart = JSON.parse(flowChartJson);

    var node = flowChart.node;
    $.each(node, function (index, elem) {

        droppedElement = document.getElementById(id);

        var id = elem.id;
        var classes = elem.class;
        // var positionTop = elem.position.top;
        var asName = elem.name;
        var kind = elem.kind;

        if (classes == "streamdrop ui-draggable") {
            var selectedStream = elem.predefinedStream;
            alert("elem id: " + id + "\nclass:" + classes + "\nasName:" + asName + "\nkind:" + kind + "\nselected:" + selectedStream);
            if (kind == "import") {
                createdImportStreamArray[id - 1][0] = id;
                createdImportStreamArray[id - 1][1] = selectedStream;
                createdImportStreamArray[id - 1][2] = asName;
                createdImportStreamArray[id - 1][3] = "Import";
            }
            alert("Id of element parsed: " + id + "\nclass: " + classes /*+ "\npositionTop: " + positionTop*/);
            var newAgent = $('<div>').attr('id', id).addClass('streamdrop');
            var prop = $('<a onclick="doclick(this)"><b><img src="../Images/settings.png" class="settingsIconLoc"></b></a> ').attr('id', (id + '-prop'));
            var showIcon = $('<img src="../Images/Import.png" class="streamIconloc"></b></a> ').attr('id', (id));
            var conIcon = $('<img src="../Images/connection.png" onclick="connectionShowHideToggle(this)" class="showIconDefined"></b></a> ').attr('id', (id + 'vis'));
            newAgent.text(asName).append('<a class="boxclose" id="boxclose"><b><img src="../Images/Cancel.png"></b></a> ').append(showIcon).append(conIcon).append(prop);
            dropCompleteElement(newAgent, id, e, kind);
        }
    });
}

如果正确检索了以前保存的Json输出,我应该获得类似于第一个警报框的数据 会对这方面的一些意见表示赞赏。

1 个答案:

答案 0 :(得分:0)

我设法通过编译为单个节点内的json对象赋值的所有代码来解决这个问题,如下所示。

node.push({
          id: idOfEl,
          class: dropElem,
          position:
          {
              top: position.top,
              left: position.left,
              bottom: position.bottom,
              right: position.right
            },
            predefinedStream: createdImportStreamArray[count][1],
            name: createdImportStreamArray[count][2],
            kind: "import"
});

现在输出符合预期。