2017-02-16 5 views
0

zur Zeit habe ich eine api, in dem Sie eine json und die api Antwort senden: die json i senden: { "instructions": [ { "A": 9, "B": 1, "move": "moveonto" }, { "A": 8, "B": 1, "move": "moveover" } ], "length": 20, "res": "null" }Gebäude JSON-String in Winkel von Form

und die api Antwort: { "instructions": [ { "A": 9, "B": 1, "move": "moveonto" }, { "A": 8, "B": 1, "move": "moveover" } ], "length": 20, "res": "Position [0] : 0Position [1] : 1 9 8Position [2] : 2Position [3] : 3Position [4] : 4Position [5] : 5Position [6] : 6Position [7] : 7Position [8] : Position [9] : Position [10] : 10Position [11] : 11Position [12] : 12Position [13] : 13Position [14] : 14Position [15] : 15Position [16] : 16Position [17] : 17Position [18] : 18Position [19] : 19" }

im a Entwicklung sehr einfache Webseite: My Webpage wenn ich auf die Schaltfläche zum Senden an den Server klicken muss ich es senden, das Problem ist, ich weiß nicht, wie man den JSON bauen, können Sie mir helfen? thx

ich habe leichte Idee:

Json = { 
    "instructions": [{ 
    "A": $scope.addA, 
    "B": $scope.addB, 
    "move": $scope.addMov 
    }, { 
    "A": $scope.addA, 
    "B": $scope.addB, 
    "move": $scope.addMov 
    }], 
    "length": $scope.blockLength, 
    "res": null 
}; 

und ich es senden:

$http.post("http://localhost:56493/api/BlocksProblem", Json) 
    .then(function (data) { 
    $scope.result = data; 
    }, function (response) { 
    $scope.result = response; 
    }); 

Vielen Dank für Ihre Zeit durch alle es lesen.

+0

erhalten Sie einen Fehler? – Coder

Antwort

0

Sie "bauen" den JSON nicht unbedingt im typischen Sinne. Wenn Sie Objektdaten senden, konvertieren Sie angularjs mit json für Sie.

zum Beispiel. Wenn ich eine Javascript-Variable wie dieses:

var J = {A:1, B:2} 

und als Daten in einem http-Post senden, würde die json wie folgt aussehen: { "A": "1", "B": "2"}

Ohne etwas zu sehen, was Ihre Server-Architektur aussieht, können Sie so etwas wie dieses

$scope.SendJson = function (Callback) { 

    $http({ 
     url: YOURURL, 
     method: 'POST', 
     dataType: 'json', 
     data: { 
      "instructions": [ 
       {"A": $scope.addA,"B": $scope.addB,"move": $scope.addMov}, 
       {"A": $scope.addA,"B": $scope.addB,"move": $scope.addMov}], 
      "length": $scope.blockLength, 
      "res": null} 
    }) 
    .then(function (data) { 
       $scope.result = data; 
      }, function (response) { 
       $scope.result = response; 
      }); 

    }) 

auch tun, Sie nicht den Schlüsselwert zitieren müssen. Es ist implizit

Mit allem, was gesagt wird, vergewissern Sie sich, dass das Paket korrekt in Ihren Entwicklertools erstellt wird.

0

durch Stackoverflow zu lesen fand ich diese:

How do I create JavaScript array (JSON format) dynamically?

so bauen i Mine mit:

//here i save the values of A,B and the moves, that i get from the form 
    var serverMove = []; 
    var serverA = []; 
    var serverB = []; 
    //create the json 
    var jsonS = { 
      instructions: [], 
      "length": $scope.blockLength, 
      "res": "" 
    }; 
     //dynamically fill only the instrucions array part 
     for (var i = 0; i < serverA.length; i++) { 
      jsonS.instructions.push({ 
      "A": serverA[i], 
      "B": serverB[i], 
      "move": serverMove[i] 
      }) 
     } 

das Ergebnis json ist so etwas wie:

{ 
    "instructions": [ 
     { 
      "A": 1 
      , "B": 3, 
      "move": 
      "moveonto" 
     }, 
     { 
      "A": 5, 
      "B": 9, 
      "move": "pileover" 
     } 
     ], 
     "length": 22, 
     "res": "" 
} 

i jemand hoffen findet es nützlich

Verwandte Themen