将JSON.STRINGIFY转换为json数组长度不计算

时间:2017-05-23 07:01:32

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

当我上传应保存到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的长度我怎么能得到它。任何人都可以告诉我

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

试试这个:

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)

答案 2 :(得分:1)

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

答案 3 :(得分:1)

根据我对上述代码的理解,您将获得JSON字符串格式的响应,因此您无法对其执行JSON操作。尝试使用JSON.parse方法将json字符串转换为json对象.Below是节点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);

}