簡體   English   中英

檢索Json對象值

[英]Retrieve Json object values

我目前正致力於根據提供的Json輸入創建模型,反之亦然。

問題是當我嘗試檢索諸如predefinedStreamnamekind的對象時,雖然前兩個idclasses在警報中正確顯示,但它顯示未定義。

保存了Json輸出

救了Json

生成警報消息

警報

提供Json時加載元素

當我將以前生成的代碼復制到文本區域並單擊加載時,我得到以下內容

UI

警告上述對象

警報

在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輸出,我應該得到類似於第一個警報框的數據。在這方面會有一些意見。

我設法通過編譯將值分配給單個節點內的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"
});

現在輸出正如預期的那樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM