使用jquery嵌套Json Data

时间:2017-02-08 14:27:57

标签: javascript jquery json csv

我有一个csv文件中的这些数据,我必须在依赖的下拉列表中使用jquery。我无法弄清楚是否有可能将我收到的数据嵌套到我已编码的内容中。

CSV文件

Banco   Tarjeta Cuotas  Medio_Pago  Coeficiente TEA      CFT
Santander Visa    1   modulodepago2    1        0.00%    0.00%
Santander Visa    1        nps        1.0262    18.56%  22.84%
Frances   Visa    1   modulodepago2    1         0.00%  0.00%
Frances  Master   2        nps        1.0262    18.56%  22.84%

我的json数据就像这样

 [{"banco":"Santander","tarjeta":"Visa","cuotas":"1","medio_pago":"modulodepago2",
 "coeficiente":"1","tea":"0.00%","cft":"0.00%"},
 {"banco":"Santander","tarjeta":"Visa","cuotas":"1","medio_pago":"nps",
 "coeficiente":"1.0262","tea":"18.56%","cft":"22.84%"} ...
  etc...

有没有办法可以像这样嵌套这个json数据(+添加唯一名称和id)?

var myJson = {
  "banco": [
      {
          "name": "Santander",
          "id": "Santander",
          "tarjeta": [
              {
                  "name": "Visa",
                  "id": "SantanderVisa",
                  "cuotas": [
                      {
                          "name": "1",
                          "id": "SantanderVisa1",
                          "medio_pago": "modulodepago2"
                          "coeficiente": "1",
                          "tea": "0.00%",
                          "cft": "0.00%",
                      },
                      {
                          "name": "1",
                          "id": "SantanderVisa2",
                          "medio_pago": "nps"
                          "coeficiente": "1.0262",
                          "tea": "18.56%",
                          "cft": "22.84%",
                      }
                  ]
              }
          ]
      },
      {
          "name": "Frances",
          "id": "Frances",
          "tarjeta": [
              {
                  "name": "Visa",
                  "id": "FrancesVisa",
                  "cuotas": [
                      {
                          "name": "1",
                          "id": "FrancesVisa1",
                          "medio_pago": "modulodepago2"
                          "coeficiente": "1",
                          "tea": "0.00%",
                          "cft": "0.00%",
                      }
                  ]
              },
              {
                  "name": "Master",
                  "id": "FrancesMaster",
                  "cuotas": [
                      {
                          "name": "2",
                          "id": "FrancesMaster2",
                          "medio_pago": "nps"
                          "coeficiente": "1.0262",
                          "tea": "18.56%",
                          "cft": "22.84%",
                      }
                  ]
              }
          ]
      }
  ]
}

2 个答案:

答案 0 :(得分:1)

尝试这样的事情 你得到所有medio_pago用于你只使用对象名称的其他对象。 我没有测试过,但我相信这对你有用。

var Json = ...

$.each(Json, function(i, item) {
    alert(myJson[i].banco.tarjeta.cuotas.medio_pago);
});

答案 1 :(得分:1)

您需要按键分组。一种简单的方法是使用LodashUnderscore.js

我使用Papa Parse将CSV数据转换为JSON。

var csvData = $('#csv-data').text().trim();
var jsonData = Papa.parse(csvData, { delimiter:',', header:true }).data;

var transformedJson = {
  banco : _.chain(jsonData)
  .groupBy('Banco')
  .toPairs()
  .map(banco => {
    return {
      name : banco[0],
      id: banco[0],
      tarjeta : _.chain(banco[1])
      .groupBy('Tarjeta')
      .toPairs()
      .map(tarjeta => {
        return {
          name: tarjeta[0],
          id: banco[0] + tarjeta[0],
          cuotas: _.map(tarjeta[1], cuota => {
            return {
              name: cuota['Cuotas'],
              id: banco[0] + tarjeta[0] + cuota['Cuotas'],
              medio_pago: cuota['Medio_Pago'],
              coeficiente: cuota['Coeficiente'],
              tea: cuota['TEA'],
              cft: cuota['CFT']
            }
          })
        };
      })
    }
  }).value()
}

console.log(JSON.stringify(transformedJson, null, 2));
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.1.4/papaparse.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<textarea id="csv-data" style="display:none" rows="5" cols="72">
Banco,Tarjeta,Cuotas,Medio_Pago,Coeficiente,TEA,CFT
Santander,Visa,1,modulodepago2,1,0.00%,0.00%
Santander,Visa,1,nps,1.0262,18.56%,22.84%
Frances,Visa,1,modulodepago2,1,0.00%,0.00%
Frances,Master,2,nps,1.0262,18.56%,22.84%
</textarea>