解析JSON文件并仅提取数字数组

时间:2017-01-19 03:59:12

标签: javascript json

我需要解析一些JSON个文件,然后只提取数组。此外,我必须接受只有一个数字数组或一个更深层次的JSON文件(我相信它们被称为直接数组)。 另外我不知道JSON文件是否包含字符串,布尔值或其他类型。

我的问题是识别数字数组,当我检查他们的类型时,我会得到object

我不允许使用任何额外的库。这个问题有没有标准的解决方案。

file1.json

[1,2,3,4]

输出:

1,2,3,4

file2.json

{a:'4',b:true,c:[5,6,7]}

输出:

5,6,7

file3.json

{a:[1,'2',3],b:2,c:['1','2','3']}

输出:

[]

我需要在Promise中使用结果,因此如果没有数字数组或无效的json文件,则承诺将fulfill乘以数字和reject。< / p>

2 个答案:

答案 0 :(得分:1)

由于您正在对对象执行浅迭代,因此您只需要检查它是对象还是数组,然后相应地检查数字数组。给定一个数组,您可以迭代元素并检查每个对象是否为var array1Example = [1, 2, 3]; var array2Example = [1, '2', 3, 'a']; var object1Example = { a: '4', b: true, c: [5, 6, 7] }; var object2Example = { a: [1, '2', 3], b: 2, c: ['1', '2', '3'] } function isArrayOfNumbers(arr) { for (var i = 0; i < arr.length; i++) { if (typeof arr[i] !== 'number') { return false; } } return true; } function getNumberArrays(obj) { var numArrays = []; if (Array.isArray(obj)) { if (isArrayOfNumbers(obj)) { numArrays.push(obj); } } else if ((typeof obj === "object") && (obj !== null)) { for (var key in obj) { if (Array.isArray(obj[key]) && isArrayOfNumbers(obj[key])) { numArrays.push(obj[key]); } } } return numArrays; } console.log(getNumberArrays(array1Example)); console.log(getNumberArrays(array2Example)); console.log(getNumberArrays(object1Example)); console.log(getNumberArrays(object2Example));运算符的数字。下面的函数接受一个对象,并在对象中输出一个数组数组。

&#13;
&#13;
var results = from p in pending
              group p by p.ContactID into g
              let amount = g.Sum(s => s.Amount)
              select new PaymentItemModel
              {
                  ContactID = g.ContactID, // <--- Error here
                  Amount = amount
              };
&#13;
&#13;
&#13;

答案 1 :(得分:1)

此方法使用递归来检查嵌入式数组。

var a = ['[1,2,3,4]','{"a":"4","b":true,"c":[5,6,7]}','{"a":[1,"2",3],"b":2,"c":["1","2","3"]}','{xds}'];
var i,j,l = a.length;

function testForArray(v,depth) {
        var e,f;

        // Ensure this is not deeper than the first level
        if (depth > 1) {
                return false;
        }

        // Check if v is an array
        if (Array.isArray(v)) {
                // Use filter to find any elements which are not numeric
                // In this case, "2" is considered a string, not a number
                f = v.filter( function(e) {
                        return (isNaN(e) || typeof e === "string");
                });
                // If the filtered array is empty, all elements were numeric
                if (f.length === 0) {
                        return true;
                }
                // The filtered array was not empty
                return false;
        } else {

                // If v is an object
                if (typeof v === "object") {
                        // Loop through all the properties
                        for (e in v) {
                                // Check if each property is an array, incrementing the depth
                                if (testForArray(v[e],depth+1) === false) {
                                        // If the array is non-numeric, fail
                                        return false;
                                }
                        }
                        // Return true if it has not yet failed
                        return true;
                }
        }
        // Return null if the element tested was not an array or object
        return null;
}

// Test the function    
for (i = 0; i < l; i++) {
        console.log(a[i]);
        try {
                j = JSON.parse(a[i]);
                console.log(testForArray(j,0));
        } catch (e) {
                console.log('caught: '+e);
        }
}