Build json with several arrays

0

I have an api that receives this json:

{
    "instructions":[
        {
            "A":9,
            "B":1,
            "move":"moveover"
        },
        {
            "A":8,
            "B":1,
            "move":"moveover"
        }
        ],
        "length":10,
        "res":""
}

And the next one comes back to me:

{
  "instructions": [
    {
      "A": 9,
      "B": 1,
      "move": "moveover"
    },
    {
      "A": 8,
      "B": 1,
      "move": "moveover"
    }
  ],
  "length": 10,
  "res": "Position [0] :  0Position [1] :  1 9 8Position [2] :  2Position [3] :  3Position [4] :  4Position [5] :  5Position [6] :  6Position [7] :  7Position [8] : Position [9] : "
}

I take the data from the following web page / form: What I do is save the values of A, B and movements in an array, for example:

//lo que tendria en memoria 
var A=[1,3,5,8];
var B=[5,6,7,10];
var moves=[moveinto,moveover,pileover,pileinto];
//añadiendo los valores al array 
$scope.addMove = function () {
     A.push($scope.addA);
     B.push($scope.addB);
     moves.push($scope.addMov);
}

my question is how to assemble the json to be able to send it by post?

One idea I had was:

for (var i = 0; i < serverA.length; i++) {
      objetoJson[i] = {
        "instructions": [{
          "A": serverA[i],
          "B": serverB[i],
          "move": serverMove[i]
        }],
        "length": $scope.blockLength,
        "res": null
      };

but it did not work for me, now what I did was send it manually and if it works for me:

json = {
      "instructions": [{
          "A": 9,
          "B": 1,
          "move": "moveover"
        },
        {
          "A": 8,
          "B": 1,
          "move": "moveover"
        }
      ],
      "length": 10,
      "res": ""
    }
    $http.post("http://localhost:56493/api/BlocksProblem", json)
      .then(function (data) {
        $scope.result = data;
      }, function (response) {
        $scope.result = response;
      });

Could you tell me how to put my json together?

    
asked by Angel Silva 16.02.2017 в 23:25
source

3 answers

1

Your problem is that you are generating an arrangement of objects when what you have to generate is an object with a key instructions that has as value an arrangement, this is arranged generating the arrangement of instructions and then generating the object with the keys that you need:

First, since your controller already generates the structure of the instructions as follows:

var instructions = [];

$scope.addMove = function () {

    instructions.push({
        A: $scope.addA,
        B: $scope.addB,
        move: $scope.addMov
    });
}

later create your json in the following way:

json = {
    instructions: instructions,
    length: $scope.blockLength,
    res: ""
}

And you're ready to send it to your API.

Greetings!

    
answered by 17.02.2017 в 00:12
0

I recommend you to manage everything as an array and then at the moment you need the Json, you will pair it in this way:

var json = [1,2,3,4];
var data = JSON.parse(json);
    
answered by 17.02.2017 в 01:31
0

ALREADY SOLVED, THANK YOU TO ALL, THIS WAS MY SOLUTION:

reading is stackoverflow in English I found this:

link

So I proceeded to create mine:

    //Guardo los valores que obtengo del form en arrays 
    var serverMove = [];
    var serverA = [];
    var serverB = [];
    //create the json 
    var jsonS = {
          instructions: [],
          "length": $scope.blockLength,
          "res": ""
    };
       //lleno dinamicamente unicamente la parte de instrucciones
        for (var i = 0; i < serverA.length; i++) {
          jsonS.instructions.push({
            "A": serverA[i],
            "B": serverB[i],
            "move": serverMove[i]
          })
        }

the resulting json is something like this:

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

I hope it will be useful to someone

    
answered by 17.02.2017 в 00:31