Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解析JSON文件并仅提取数字数组_Javascript_Json - Fatal编程技术网

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

Javascript 解析JSON文件并仅提取数字数组,javascript,json,Javascript,Json,我需要解析一些JSON文件,然后只提取数字数组。此外,我必须接受只有一个数字数组或更深一层的JSON文件(我认为它们被称为直接数组)。 此外,我不知道JSON文件是否包含字符串、布尔值或其他类型 我的问题是识别数字数组,当我检查它们的类型时,我将得到对象 我不允许使用任何额外的图书馆。这个问题有没有标准的解决方案 file1.json [1,2,3,4] {a:'4',b:true,c:[5,6,7]} {a:[1,'2',3],b:2,c:['1','2','3']} 输出: 1,2,3

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

我的问题是识别数字数组,当我检查它们的类型时,我将得到
对象

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

file1.json

[1,2,3,4]
{a:'4',b:true,c:[5,6,7]}
{a:[1,'2',3],b:2,c:['1','2','3']}
输出:

1,2,3,4
5,6,7
[]
file2.json

[1,2,3,4]
{a:'4',b:true,c:[5,6,7]}
{a:[1,'2',3],b:2,c:['1','2','3']}
输出:

1,2,3,4
5,6,7
[]
file3.json

[1,2,3,4]
{a:'4',b:true,c:[5,6,7]}
{a:[1,'2',3],b:2,c:['1','2','3']}
输出:

1,2,3,4
5,6,7
[]

我需要在一个
Promise
中使用结果,这样Promise将
通过数字的总和来实现
,如果没有数字数组或无效的json文件,那么
将拒绝

因为您正在对对象进行浅层迭代,您只需要检查它是一个对象还是一个数组,然后相应地检查数字数组。给定一个数组,您可以迭代元素,并使用
typeof
运算符检查每个对象是否为数字。下面的函数接收对象并输出对象中的数字数组数组

var array1Example=[1,2,3];
var array2Example=[1,'2',3,'a'];
变量object1Example={
a:‘4’,
b:是的,
c:[5,6,7]
};
变量object2Example={
答:[1,'2',3],
b:2,
c:['1','2','3']
}
函数IsArrayOfNumber(arr){
对于(变量i=0;ilog(getNumberArray(object2Example))此方法使用递归检查嵌入式数组

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);
        }
}
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.长度;
函数testForArray(v,深度){
变量e,f;
//确保其深度不超过第一层
如果(深度>1){
返回false;
}
//检查v是否为数组
if(数组isArray(v)){
//使用过滤器查找任何非数字元素
//在本例中,“2”被视为字符串,而不是数字
f=v.过滤器(功能(e){
return(isNaN(e)| typeof e==“string”);
});
//如果筛选的数组为空,则所有元素均为数字
如果(f.length==0){
返回true;
}
//筛选的数组不是空的
返回false;
}否则{
//如果v是一个对象
如果(v的类型==“对象”){
//循环遍历所有属性
对于(e/v){
//检查每个属性是否为数组,增加深度
if(testForArray(v[e],深度+1)==false){
//如果数组不是数字,则失败
返回false;
}
}
//如果尚未失败,则返回true
返回true;
}
}
//如果测试的元素不是数组或对象,则返回null
返回null;
}
//测试功能
对于(i=0;i
您是否可以发布您最初的尝试?在不了解问题的很多背景的情况下,请看一看“没有”,因为这个问题没有标准的解决方案。你需要写一些代码。所以你想在对象中查找所有只包含数字的数组吗?@MaxSindwani是的。但并非全部。只有0级和1级数字数组。为什么在
file3.json
中输出是
[]