【问题标题】:Convert JSON.STRINGIFY to json array length is not calculating将 JSON.STRINGIFY 转换为 json 数组长度未计算
【发布时间】:2017-10-22 23:53:42
【问题描述】:

您好,当我上传应该保存到 mongodb(数据库)的 excel 时,我有 excel 上传功能。我已将 excel 单元格值作为 json stringify 并将其转换为解析。现在我不知道如何获得那个 json 长度值。

这里我附上了我的代码

    function to_json(workbook) {
    debugger;
    console.log(workbook);
        var result = {};
        workbook.SheetNames.forEach(function(sheetName) {
    $scope.Sheetname=sheetName;  // here sheet name is excel sheet name

        MainSheetName=sheetName;
            var roa = X.utils.sheet_to_json(workbook.Sheets[sheetName]);
            if(roa.length > 0){
                result[sheetName] = roa;
            }
        });
        return result;

    }




    var HTMLOUT = document.getElementById('htmlout');
    function to_html(workbook) {
        HTMLOUT.innerHTML = "";
        workbook.SheetNames.forEach(function(sheetName) {
            var htmlstr = X.write(workbook, {sheet:sheetName, type:'binary', bookType:'html'});
            HTMLOUT.innerHTML += htmlstr;
        });
    }

    var tarea = document.getElementById('b64data');
    function b64it() {
        if(typeof console !== 'undefined') console.log("onload", new Date());
        var wb = X.read(tarea.value, {type: 'base64',WTF:wtf_mode});
        process_wb(wb);
    }
    window.b64it = b64it;

    var OUT = document.getElementById('out');
    var global_wb;
    function process_wb(wb) {
        global_wb = wb;
        var output = "";
        var output1 = "";
        switch(get_radio_value("format")) {

            case "json":
                output = JSON.stringify(to_json(wb), 2, 2);

                output1=JSON.parse(output);
                break;
            case "form":
                output = to_formulae(wb);
                break;
            case "html": return to_html(wb);
            default:
                output = to_csv(wb);
        }
        if(OUT.innerText === undefined) OUT.textContent = output;


        else OUT.innerText = output;
        debugger;

        $scope.MainInfo=output1;         // this return whole data
         console.log(output1);
        $scope.jsondata=output1.Sheet1[0].length;// this returns undefined. Here sheet name is excel file sheetName it will taken from excel.
console.log($scope.jsondata);

我的输出是这样的

{
  "Sheet1": [
    {
      "Name": "xxx",
      "Age": "22",
      "Salary": "222222"
    },
    {
      "Name": "yyy",
      "Age": "23",
      "Salary": "232323"
    },
    {
      "Name": "zzz",
      "Age": "23",
      "Salary": "232323"
    }
  ]
}

现在我想要 Sheet1 的长度,我怎样才能得到它。谁能告诉我

【问题讨论】:

    标签: angularjs json node.js mean-stack meanjs


    【解决方案1】:

    如果您要将输出分配给 obj,则获取 Sheet1 的长度就像转到 obj.Sheet1.length 一样简单

    【讨论】:

      【解决方案2】:

      试试这个:

      var output = {
          "Sheet1": [{
              "Name": "xxx",
              "Age": "22",
              "Salary": "222222"
          }, {
              "Name": "yyy",
              "Age": "23",
              "Salary": "232323"
          }, {
              "Name": "zzz",
              "Age": "23",
              "Salary": "232323"
          }]
      };
      var newArr = $.grep(output1.Sheet1, function( value, index ) {
          if(n.Name == "xxx") {
              return n;
          }
      });
      console.log(newArr)
      

      【讨论】:

      • 我很困惑,你为什么把它复杂化了?
      【解决方案3】:

      您可以使用 angular.fromJson(yourJson).length 功能。 在你的情况下,它应该是 angular.fromJson($scope.jsondata).length。

      【讨论】:

        【解决方案4】:

        根据我对上述代码的理解,您正在收到 JSON 字符串格式的响应,因此您无法对其执行 JSON 操作。尝试使用 JSON.parse 方法将 json 字符串转换为 json 对象。下面是节点 js 中的示例代码。

        var parseData = JSON.parse(output1);
        

        var parsedata1 = parseData.Sheet1;

        for(var namevalue=0;namevalue<parsedata.length;namevalue++){
        console.log("Name value: "+parsedata1[namevalue].Name);
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-03-04
          • 2018-07-06
          • 2010-11-10
          • 1970-01-01
          • 2015-08-04
          • 2016-01-22
          相关资源
          最近更新 更多