qq_36437907
ybqq_0221
2019-07-06 16:41
采纳率: 100%
浏览 2.2k

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条回答 默认 最新

  • qq_35235940
    听楼一夜雨 2019-07-06 16:48
    已采纳

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

    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_36022290
    zhenyu-s5 2019-07-06 16:51

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

    点赞 评论
  • qq_23126581
    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]); //这个可以取到数组中的值,我这里取得是第一个值。
    
    
    点赞 评论
  • wojiushiwo945you
    毕小宝 2019-07-06 18:38

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

    点赞 评论

相关推荐