2016-09-12 3 views
1

現在、Json入力に基づいてモデルを作成しています。逆も同様です。Jsonオブジェクト値を取得する

問題は、私は、このようなpredefinedStreamnamekindなどのオブジェクトを取得しようとすると、最初の二idclassesが正しく警告に表示されているが、それは未定義を示しています。

保存JSON出力

saved Json

警告メッセージが

alert JSONが設けられている

負荷要素を生成

私はテキストエリアに、以前に生成されたコードをコピーして、負荷をクリックすると、私はJSON形式で次

ui

警告上記の目的のために

alert

セーブデータ取得オブジェクト

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オブジェクトの値を取得します。

+0

あなたの 'json'ブロックのどこにでも' undefined'を見つけることができません! –

+0

あなたのflowChartJson Jsonは少し疑わしいですね。 Jsonのフォーマットについてはこちらをご覧ください - https://www.copterlabs.com/json-what-it-ishow-it-works-how-to-use-it/ – Tasos

答えて

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

出力が期待通りです。

関連する問題