获取json数组中对象的对象值

时间:2017-11-29 13:01:14

标签: arrays json object

我真的需要帮助!如何从我的json数据中获取“情绪”键的值?我得到结果[对象对象],我知道我正在错误地进入这个键,因为它是对象,但不知道它的解决方案是什么..你能帮忙吗?

 data = { 
                 "happiness_position": 80,          
                 "sentiment": {"ANGER": 84.0, "DEPRESION": 26.0
        }

        for (var i = 0; i < artists.length; i++){
                var obj = artists[i];

                if(obj.nane == uservalue){

                    document.getElementById("voc").innerHTML = obj.happiness_position;
                    document.getElementById("emo").innerHTML = obj.sentiment;

                }
            }

1 个答案:

答案 0 :(得分:1)

尝试依靠情绪做动态得到它的关键:

 data = { 
                     "happiness_position": 80,          
                     "sentiment": {"ANGER": 84.0, "DEPRESION": 26.0
            }

        for (var i = 0; i < artists.length; i++){
                var obj = artists[i];

                if(obj.nane == uservalue){

                    document.getElementById("voc").innerHTML = obj.happiness_position;
                    document.getElementById("emo").innerHTML = ""
                    for(var key in obj.sentiment)
                    {
                       document.getElementById("emo").innerHTML+= key + ":" + obj.sentiment[key] + "\n"
                    }


                }
            }