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

Convert JSON.STRINGIFY to json array length is not calculating

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

here i have attached my code

    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);

my output is showing like this

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

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

如果您要将输出分配给 obj,那么获取 Sheet1 的长度就像 obj.Sheet1.length

一样简单

试试这个:

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)

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

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

}