json对象的属性值获取不了?为undefined?
var data1 = {
                "raster_metainfo": [0,0,0,0,0,0,0],
                "navigation_metainfo": [0,0,0,0,0,0,0],
                "thematic_metainfo": [0,0,0,0,0,0,0],
                "text_metainfo":  [0,0,0,0,0,0,0],
                "video_metainfo":  [0,0,0,0,0,0,0],
                "database_metainfo": [0,0,0,0,0,0,0],
                "service_metainfo":  [0,0,0,0,0,0,0]
            }
            jsonData = JSON.stringify(data1);

            var a ='navigation_metainfo';
                        console.log(data1.a);

4个回答

你已经吧对象转成字符串了

var data1 = {
                "raster_metainfo": [0,0,0,0,0,0,0],
                "navigation_metainfo": [0,0,0,0,0,0,0],
                "thematic_metainfo": [0,0,0,0,0,0,0],
                "text_metainfo":  [0,0,0,0,0,0,0],
                "video_metainfo":  [0,0,0,0,0,0,0],
                "database_metainfo": [0,0,0,0,0,0,0],
                "service_metainfo":  [0,0,0,0,0,0,0]
            };
            var a ='navigation_metainfo';
            console.log(data1.a);
            console.log(data1[a]);
qq_36437907
qq_36437907 我真的是个智障啊感觉哈哈哈哈,感谢
9 个月之前 回复
qq_35235940
听楼一夜雨 自己看区别吧
9 个月之前 回复

你这么写肯定不对啊,编译成的是data1里面的a,但是data1.a根本不存在,写成这样就行了:
console.log(data1[a]);

var data1 = {
                "raster_metainfo": [0,0,0,0,0,0,0],
                "navigation_metainfo": [0,0,0,0,0,0,0],
                "thematic_metainfo": [0,0,0,0,0,0,0],
                "text_metainfo":  [0,0,0,0,0,0,0],
                "video_metainfo":  [0,0,0,0,0,0,0],
                "database_metainfo": [0,0,0,0,0,0,0],
                "service_metainfo":  [0,0,0,0,0,0,0]
            }


               console.log(data1.navigation_metainfo);    //这个是可以取到整个navigation_metainfo 的数组
                             console.log(data1.navigation_metainfo[0]); //这个可以取到数组中的值,我这里取得是第一个值。

这里的 a 是一个变量,不能直接 .a 可以通过data1 [a] 获取这个属性值。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问