How do I convert a json to a two-dimensional array by javascript or jquery?

1

I have a problem with a json, because I have to transform it into a two-dimensional array or with this format [[0,0], [0,0]], at the moment only for normal arrays, and I also use it know how to do with this format, which in essence is the same [[0,0]], [[0,0]].

Here I will leave the code of how I did it for a normal arrangement:

Javascript

 $(function () {
        var dataTem = [];
        var dataVir = [];
        var dataDef = [];
        var dataCR = [];
        var dataReg = [];
        var tickData = [];


        $.ajax({
            type: 'GET',
            url: '../../../../API/hola',
            async: false,
            contentType: 'application/json',
            success: function (data) {

                dataTem.push([0,0]);
                dataVir.push([0,0]);
                dataDef.push([0,0]);
                dataCR.push([0,0]); //[[0, 3], [1, 19]]  [[0,12]]
                dataReg.push([0,0]);

                $.each(data, function (key, val) {
                    dataTem.push([key + 1, val.imp_PTemporal]);
                    dataVir.push([key + 1, val.imp_PVirtual]);
                    dataDef.push([key + 1, val.imp_PDefinitivos]);
                    dataCR.push([key + 1, val.imp_PCambioRegimen]);
                    dataReg.push([key + 1, val.imp_PRegularizacion]);
                    tickData.push([key + 1, val.anio + '/' + val.mes]);
                });
            }
        });

JSON

{
   "ArrayOfIXTipoOperacionImportacion":{
      "IXTipoOperacionImportacion":[
         {
            "anio":"2015",
            "mes":"NOV",
            "mesNo":"11",
            "imp_PTemporal":"4626095.64000",
            "diferenciaMesAnteriorIMPTemp":"-2966880.12000",
            "porcentajeDiferenciaIMPTemp":"-39.07401",
            "imp_PTotalTemportal":"11",
            "imp_PVirtual":"3292554.70000",
            "diferenciaMesAnteriorIMPVirt":"-698600.84000",
            "porcentajeDiferenciaIMPVirt":"-17.50372",
            "imp_PTotalVirtual":"47",
            "imp_PDefinitivos":"351483.72000",
            "diferenciaMesAnteriorIMPDef":"-44040.60000",
            "porcentajeDiferenciaIMPDef":"-11.13474",
            "imp_PTotalDefinitivos":"38",
            "imp_PRegularizacion":"0.00000",
            "diferenciaMesAnteriorIMPReg":"0.00000",
            "porcentajeDiferenciaIMPReg":"-100.00000",
            "imp_PTotalRegularizacion":"0",
            "imp_PCambioRegimen":"59518.88000",
            "diferenciaMesAnteriorIMPCamReg":"27389.00000",
            "porcentajeDiferenciaIMPCamReg":"85.24464",
            "imp_PTotalCambioRegimen":"20"
         },
         {
            "anio":"2015",
            "mes":"DIC",
            "mesNo":"12",
            "imp_PTemporal":"5090159.07000",
            "diferenciaMesAnteriorIMPTemp":"464063.43000",
            "porcentajeDiferenciaIMPTemp":"10.03143",
            "imp_PTotalTemportal":"13",
            "imp_PVirtual":"4041906.34000",
            "diferenciaMesAnteriorIMPVirt":"749351.64000",
            "porcentajeDiferenciaIMPVirt":"22.75897",
            "imp_PTotalVirtual":"42",
            "imp_PDefinitivos":"307289.87000",
            "diferenciaMesAnteriorIMPDef":"-44193.85000",
            "porcentajeDiferenciaIMPDef":"-12.57351",
            "imp_PTotalDefinitivos":"33",
            "imp_PRegularizacion":"1970.05000",
            "diferenciaMesAnteriorIMPReg":"1970.05000",
            "porcentajeDiferenciaIMPReg":"196905.00000",
            "imp_PTotalRegularizacion":"2",
            "imp_PCambioRegimen":"34533.54000",
            "diferenciaMesAnteriorIMPCamReg":"-24985.34000",
            "porcentajeDiferenciaIMPCamReg":"-41.97885",
            "imp_PTotalCambioRegimen":"9"
         },
         {
            "anio":"2016",
            "mes":"ENE",
            "mesNo":"01",
            "imp_PTemporal":"7259696.91000",
            "diferenciaMesAnteriorIMPTemp":"2169537.84000",
            "porcentajeDiferenciaIMPTemp":"42.62220",
            "imp_PTotalTemportal":"15",
            "imp_PVirtual":"3098137.88000",
            "diferenciaMesAnteriorIMPVirt":"-943768.46000",
            "porcentajeDiferenciaIMPVirt":"-23.34959",
            "imp_PTotalVirtual":"43",
            "imp_PDefinitivos":"402000.83000",
            "diferenciaMesAnteriorIMPDef":"94710.96000",
            "porcentajeDiferenciaIMPDef":"30.82137",
            "imp_PTotalDefinitivos":"48",
            "imp_PRegularizacion":"0.00000",
            "diferenciaMesAnteriorIMPReg":"-1970.05000",
            "porcentajeDiferenciaIMPReg":"-100.00000",
            "imp_PTotalRegularizacion":"0",
            "imp_PCambioRegimen":"87980.27000",
            "diferenciaMesAnteriorIMPCamReg":"53446.73000",
            "porcentajeDiferenciaIMPCamReg":"154.76760",
            "imp_PTotalCambioRegimen":"8"
         }
      ],
      "_xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
      "_xmlns:xsd":"http://www.w3.org/2001/XMLSchema"
   }
}

Any answer is appreciated.

    
asked by Guillermo Navarro 03.11.2016 в 15:25
source

2 answers

0

First of all, take it and parse it to an object in order to work it:

success: function (data) {
    var obj = $.parseJSON(data)
}

The rest of the question is that I do not understand it, could you explain it in another way? ..

    
answered by 03.11.2016 в 15:31
0

Following your code I made the following example, what happens is that in the json the information that you are taking in the cycle is in the level jsonData.ArrayOfIXTipoOperacionImportacion.IXTipoOperacionImportacion of json , I am not sure if it is what you need, but you can check it to see if it can be of any use, greetings.

 var jsonData = [];

 $(document).ready(function() {
   jsonData = {
     "ArrayOfIXTipoOperacionImportacion": {
       "IXTipoOperacionImportacion": [{
         "anio": "2015",
         "mes": "NOV",
         "mesNo": "11",
         "imp_PTemporal": "4626095.64000",
         "diferenciaMesAnteriorIMPTemp": "-2966880.12000",
         "porcentajeDiferenciaIMPTemp": "-39.07401",
         "imp_PTotalTemportal": "11",
         "imp_PVirtual": "3292554.70000",
         "diferenciaMesAnteriorIMPVirt": "-698600.84000",
         "porcentajeDiferenciaIMPVirt": "-17.50372",
         "imp_PTotalVirtual": "47",
         "imp_PDefinitivos": "351483.72000",
         "diferenciaMesAnteriorIMPDef": "-44040.60000",
         "porcentajeDiferenciaIMPDef": "-11.13474",
         "imp_PTotalDefinitivos": "38",
         "imp_PRegularizacion": "0.00000",
         "diferenciaMesAnteriorIMPReg": "0.00000",
         "porcentajeDiferenciaIMPReg": "-100.00000",
         "imp_PTotalRegularizacion": "0",
         "imp_PCambioRegimen": "59518.88000",
         "diferenciaMesAnteriorIMPCamReg": "27389.00000",
         "porcentajeDiferenciaIMPCamReg": "85.24464",
         "imp_PTotalCambioRegimen": "20"
       }, {
         "anio": "2015",
         "mes": "DIC",
         "mesNo": "12",
         "imp_PTemporal": "5090159.07000",
         "diferenciaMesAnteriorIMPTemp": "464063.43000",
         "porcentajeDiferenciaIMPTemp": "10.03143",
         "imp_PTotalTemportal": "13",
         "imp_PVirtual": "4041906.34000",
         "diferenciaMesAnteriorIMPVirt": "749351.64000",
         "porcentajeDiferenciaIMPVirt": "22.75897",
         "imp_PTotalVirtual": "42",
         "imp_PDefinitivos": "307289.87000",
         "diferenciaMesAnteriorIMPDef": "-44193.85000",
         "porcentajeDiferenciaIMPDef": "-12.57351",
         "imp_PTotalDefinitivos": "33",
         "imp_PRegularizacion": "1970.05000",
         "diferenciaMesAnteriorIMPReg": "1970.05000",
         "porcentajeDiferenciaIMPReg": "196905.00000",
         "imp_PTotalRegularizacion": "2",
         "imp_PCambioRegimen": "34533.54000",
         "diferenciaMesAnteriorIMPCamReg": "-24985.34000",
         "porcentajeDiferenciaIMPCamReg": "-41.97885",
         "imp_PTotalCambioRegimen": "9"
       }, {
         "anio": "2016",
         "mes": "ENE",
         "mesNo": "01",
         "imp_PTemporal": "7259696.91000",
         "diferenciaMesAnteriorIMPTemp": "2169537.84000",
         "porcentajeDiferenciaIMPTemp": "42.62220",
         "imp_PTotalTemportal": "15",
         "imp_PVirtual": "3098137.88000",
         "diferenciaMesAnteriorIMPVirt": "-943768.46000",
         "porcentajeDiferenciaIMPVirt": "-23.34959",
         "imp_PTotalVirtual": "43",
         "imp_PDefinitivos": "402000.83000",
         "diferenciaMesAnteriorIMPDef": "94710.96000",
         "porcentajeDiferenciaIMPDef": "30.82137",
         "imp_PTotalDefinitivos": "48",
         "imp_PRegularizacion": "0.00000",
         "diferenciaMesAnteriorIMPReg": "-1970.05000",
         "porcentajeDiferenciaIMPReg": "-100.00000",
         "imp_PTotalRegularizacion": "0",
         "imp_PCambioRegimen": "87980.27000",
         "diferenciaMesAnteriorIMPCamReg": "53446.73000",
         "porcentajeDiferenciaIMPCamReg": "154.76760",
         "imp_PTotalCambioRegimen": "8"
       }],
       "_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
       "_xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
     }
   }

   //console.log(jsonData.ArrayOfIXTipoOperacionImportacion);

   correrEjemplo();

 });

 var dataCR = [];
 var dataReg = [];
 var tickData = [];
 var dataTem = [];
 var dataVir = [];
 var dataDef = [];

 function correrEjemplo() {

   dataTem.push([0, 0]);
   dataVir.push([0, 0]);
   dataDef.push([0, 0]);
   dataCR.push([0, 0]); //[[0, 3], [1, 19]]  [[0,12]]
   dataReg.push([0, 0]);

   $.each(jsonData.ArrayOfIXTipoOperacionImportacion.IXTipoOperacionImportacion, function(key, val) {
     dataTem.push([key + 1, val.imp_PTemporal]);
     dataVir.push([key + 1, val.imp_PVirtual]);
     dataDef.push([key + 1, val.imp_PDefinitivos]);
     dataCR.push([key + 1, val.imp_PCambioRegimen]);
     dataReg.push([key + 1, val.imp_PRegularizacion]);
     tickData.push([key + 1, val.anio + '/' + val.mes]);
   });

   imprimirArreglos();
 };

 function imprimirArreglos() {
   console.log('Valores para dataTem');
   $.each(dataTem, function(key, val) {
     console.log(val);

   });
   console.log('Valores para dataVir');
   $.each(dataVir, function(key, val) {
     console.log(val);

   });
   console.log('Valores para dataDef');
   $.each(dataDef, function(key, val) {
     console.log(val);

   });
   console.log('Valores para dataCR');
   $.each(dataCR, function(key, val) {
     console.log(val);

   });
   console.log('Valores para dataReg');
   $.each(dataReg, function(key, val) {
     console.log(val);

   });
 };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 19.12.2016 в 19:37