2012-11-01 91 views
1

我需要爲輸入來自html表單的JSON請求創建數據。我想要生成的數據格式如下所示。使用javascript創建JSON請求數據

{ 
    "applicationName":"Tesing", 
    "cpuCount":"12", 
    "hostdescName":"localhost", 
    "maxMemory":"0", 
    "maxWallTime":"0", 
    "minMemory":"0", 
    "nodeCount":"1", 
    "processorsPerNode":"12", 
    "serviceDesc":{ 
     "inputParams":[ 
     { 
      "dataType":"input", 
      "description":"my input", 
      "name":"myinput", 
      "type":"String" 
     }, 
     { 
      "dataType":"input", 
      "description":"my input", 
      "name":"myinput", 
      "type":"String" 
     } 
     ], 
     "outputParams":[ 
     { 
      "dataType":"output", 
      "description":"my output", 
      "name":"myoutput", 
      "type":"String" 
     }, 
     { 
      "dataType":"output", 
      "description":"my output", 
      "name":"myoutput", 
      "type":"String" 
     } 
     ] 
    } 
} 

我的代碼如下所示;

var jasonRequest = {}; 
    jasonRequest["applicationName"] = appName; 
    jasonRequest["cpuCount"] = cpuCount; 
    jasonRequest["hostdescName"] = hostName; 
    jasonRequest["maxMemory"] = maxMemory; 
    jasonRequest["maxWallTime"] = ""; //TODO 
    jasonRequest["minMemory"] = ""; //TODO 
    jasonRequest["nodeCount"] = nodeCount; 
    jasonRequest["processorsPerNode"] = ""; //TODO 

    var inArray = new Array(); 
    for(var j=0; j<inputCount; j++) { 
     var input = {}; 
     input["dataType"] = "input"; 
     input["description"] = "empty"; 
     input["name"] = $("#inputName" + j+1).val(); 
     input["type"] = $("#inputType" + j+1).val(); 

     inArray[j] = input; 
    } 

    var outArray = new Array(); 
    for(var j=0; j<outputCount; j++) { 
     var output = {}; 
     output["dataType"] = "output"; 
     output["description"] = "empty"; 
     output["name"] = $("#outputName" + j+1).val(); 
     output["type"] = $("#outputType" + j+1).val(); 

     outArray[j] = output; 
    } 

    var serviceDesc = {}; 
    serviceDesc["inputParams"] = inArray; 
    serviceDesc["outputParams"] = outArray; 

    jasonRequest["serviceDesc"] = serviceDesc; 


    alert("printing inArray"); 
    alert(inArray.toSource().toString()); 
    alert(JSON.stringify(jasonRequest)); 

此代碼創建的數據如下。我可以使用字符串並通過連接創建消息,但我認爲這不是一個很好的方法。正如你所看到的inputParams和outputParams沒有正確填充。你能建議我怎樣才能正確地產生這個信息。

{ 
    "applicationName":"Tesing", 
    "cpuCount":"12", 
    "hostdescName":"localhost", 
    "maxMemory":"0", 
    "maxWallTime":"0", 
    "minMemory":"0", 
    "nodeCount":"1", 
    "processorsPerNode":"12", 
    "serviceDesc":{"inputParams":"","outputParams":[]}} 

[編輯] 我的代碼如下所示。

$(document).ready(function(){ 
     var inputCount = 0; 
     var outputCount = 0; 

     $("p1").live("click", function(){ 
      inputCount++; 
      $(this).after("<br/>"); 
      $(this).after("Input Name   *:" + 
        "<input type=&quot;text&quot; id=&quot;inputName" + inputCount + "&quot; name=&quot;inputName" + inputCount + "&quot; value=&quot;echo_input&quot; size=&quot;50&quot;><br/>"); 

      $(this).after("Input Type   *:" + 
        "<input type=&quot;text&quot; id=&quot;inputType" + inputCount + "&quot; name=&quot;inputType" + inputCount + "&quot; value=&quot;String&quot; size=&quot;50&quot;><br/>"); 
     }); 

     $("p2").live("click", function(){ 
      $(this).after("Output Name   *:" + 
        "<input type=&quot;text&quot; id=&quot;outputName" + outputCount + "&quot; name=&quot;outputName" + outputCount + "&quot; value=&quot;echo_output&quot; size=&quot;50&quot;><br/>"); 
      $(this).after(); 

      $(this).after("Output Type   *:" + 
        "<input type=&quot;text&quot; id=&quot;outputType" + outputCount + "&quot; name=&quot;outputType" + outputCount + "&quot; value=&quot;String&quot; size=&quot;50&quot;><br/>"); 
     }); 

     $('[name="btn2"]').click(function(){ 
      var appName = $("#appName1").val(); 
      var exeuctableLocation = $("#exeuctableLocation1").val(); 
      var scratchWorkingDirectory = $("#scratchWorkingDirectory1").val(); 
      var hostName = $("#hostName1").val(); 

      var projAccNumber = $("#projAccNumber1").val(); 
      var queueName = $("#queueName1").val(); 
      var cpuCount = $("#cpuCount1").val(); 
      var nodeCount = $("#nodeCount1").val(); 
      var maxMemory = $("#maxMemory1").val(); 

      var serviceName = $("#serviceName1").val(); 
      var inputName1 = $("#inputName1").val(); 
      var inputType1 = $("#inputType1").val(); 
      var outputName = $("#outputName1").val(); 
      var outputType = $("#outputType1").val(); 

      var jsonRequest = {}; 
      jsonRequest["applicationName"] = appName; 
      jsonRequest["cpuCount"] = cpuCount; 
      jsonRequest["hostdescName"] = hostName; 
      jsonRequest["maxMemory"] = maxMemory; 
      jsonRequest["maxWallTime"] = ""; //TODO 
      jsonRequest["minMemory"] = ""; //TODO 
      jsonRequest["nodeCount"] = nodeCount; 
      jsonRequest["processorsPerNode"] = ""; //TODO 

      var inArray = []; 
      for(var j=0; j<inputCount; j++) { 
       var input = {}; 
       input["dataType"] = "input"; 
       input["description"] = "empty"; 
       input["name"] = $("#inputName" + j+1).val(); 
       input["type"] = $("#inputType" + j+1).val(); 

       inArray[j] = input; 
      } 

      var outArray = new Array(); 
      for(var j=0; j<outputCount; j++) { 
       var output = {}; 
       output["dataType"] = "output"; 
       output["description"] = "empty"; 
       output["name"] = $("#outputName" + j+1).val(); 
       output["type"] = $("#outputType" + j+1).val(); 

       outArray[j] = output; 
      } 

      var serviceDesc = {}; 
      serviceDesc["inputParams"] = inArray; 
      serviceDesc["outputParams"] = outArray; 

      jsonRequest["serviceDesc"] = serviceDesc; 


      alert("printing inArray"); 
      alert(inArray.toSource().toString()); 
      alert(JSON.stringify(jsonRequest)); 


      $.ajax({ 
       beforeSend: function(x) { 
        if (x && x.overrideMimeType) { 
         x.overrideMimeType("application/j-son;charset=UTF-8"); 
        } 
       }, 
       type: "POST", 
       dataType: "json", 
       contentType: "application/json;charset=utf-8", 
       url: "http://localhost:7080/airavata-registry-rest-services/registry/api/applicationdescriptor/build/save/test", 
       data: jasonRequest 
      }).done(function(msg) { 
         alert("Data Saved: " + msg); 
        }); 

     }); 
    }); 
</script> 
+9

旁註:誰是賈森?使用JSON來避免混淆。 –

+1

它應該這樣工作。你確定inArray和outArray是合適的填充數組嗎? (還可以使用[]代替新陣列(),但是這不應該是錯誤) – SoonDead

+2

@TimS。它必須是他所指的JSON Statham。 – SoonDead

回答

0

什麼是inputCountoutputCount?看起來像他們是零或NaN或什麼的,所以你得到空陣列。但是,您的代碼仍將打印"inputParams":[]而不是"inputParams":""

得到一個不錯的輸出(不,這將需要爲你的應用程序,僅用於調試),您可以使用JSON.stringify第三個參數。

+0

@bfavaretto我創建動態輸入字段。所以我使用計數器來跟蹤點擊次數。我編輯了我之前的帖子以獲得完整的代碼。 – user915745

+0

該問題是由於陣列沒有正確填充。謝謝。 – user915745

相關問題