Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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 使用jquery获取json对象中父对象的数量_Javascript_Jquery_Json - Fatal编程技术网

Javascript 使用jquery获取json对象中父对象的数量

Javascript 使用jquery获取json对象中父对象的数量,javascript,jquery,json,Javascript,Jquery,Json,下面是JSON数组,我想获得父对象的数量,然后在它们上运行for循环以获得每个对象的值 它应该给出总数2,因为我有两个父对象-canvas0和canvas1 {"canvas0": "{"objects": [{"type":"textbox","originX":"left","originY":"top","left":40,"top":350,"width":200,"height":20.97,"fill":"black","stroke":null,"strok

下面是JSON数组,我想获得父对象的数量,然后在它们上运行for循环以获得每个对象的值

它应该给出总数2,因为我有两个父对象-canvas0和canvas1

{"canvas0":
     "{"objects":      
 [{"type":"textbox","originX":"left","originY":"top","left":40,"top":350,"width":200,"height":20.97,"fill":"black","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","text":"ADDRESScanvasPage1","fontSize":16,"fontWeight":"normal","fontFamily":"Helvetica","fontStyle":"","lineHeight":1.16,"textDecoration":"","textAlign":"center","textBackgroundColor":"","styles":{},"minWidth":20}],"background":""}"


,"canvas1":"{"objects":[{"type":"textbox","originX":"left","originY":"top","left":40,"top":350,"width":200,"height":20.97,"fill":"black","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","text":"ADDRESScanvasPage2","fontSize":16,"fontWeight":"normal","fontFamily":"Helvetica","fontStyle":"","lineHeight":1.16,"textDecoration":"","textAlign":"center","textBackgroundColor":"","styles":{},"minWidth":20}],"background":""}"}

正如大家所说,您的JSON是无效的。minimal中相同的有效JSON如下所示

var json = {
    "canvas0": {
        "objects": [
            {
                "type": "textbox",
                "originX": "left"
            }
        ],
        "background": "#000"
    },
    "canvas1": {
        "objects": [
            {
                "type": "select",
                "originX": "right"
            }
        ]
    }
};
为了能够遍历,您不需要顶级键的计数。而且,JSON对象没有length属性。您只需在循环中使用…并相应地显示键/值。下面是一个例子,说明如何做到这一点。请记住,这只适用于您的特定情况,因为当JSON级别更改时,循环需要更改

alert ( "Length of top level keys: " + Object.keys(json).length );

for (var key in json) {

    var canvas = json[key];
    for (var key2 in canvas) {

        if (canvas[key2] instanceof Array) {
            var object = canvas[key2][0];
            for (var key3 in object) {
                alert (key3 + ": " + object[key3]);
            }
        } else {
            alert (key2 + ": " + canvas[key2]);
        }
    }
}

这是一个完整的代码。

这是一些格式糟糕的jsonPost有效json。。。没有人能读到,而且里面有语法错误,所以很难对它进行格式化!此外,您还没有提供任何解决此问题的尝试。让我们看看你试过什么。即使在编辑之后,人们也不会在这里为你做工作,这仍然是无效的JSON!循环遍历它们时不需要计数。这不是一个JSON数组,因为数组以[]开头。您有一个具有两个属性的对象:canvas0和canvas1,每个属性对象都是一个数组。您想做什么?