简体   繁体   English

如何从此json字符串获取值?

[英]How to get the value from this json string?

success: function(data){   alert(data[0].data.AVG(Rate)); }

How can I alert 7.5? 我该如何提醒7.5? I have tried data[0].data.AVG(Rate), data.data.AVG(Rate), data.AVG(Rate) 我已经尝试过data [0] .data.AVG(Rate),data.data.AVG(Rate),data.AVG(Rate)

([{"data":{"AVG(Rate)":"7.5"}}]);
alert(data[0].data["AVG(Rate)"]);

If this is what data looks like (array of objects): 如果这是什么样的数据(对象数组):

var data = [
    {
        "data": {
            "AVG(Rate)": "7.5"
        }
    }
];

The key is what your key looks like: AVG(Rate) 密钥就是您的密钥的样子: AVG(Rate)

This has parenthesis, so JS will try to call the AVG function if you try to access it with . 它带有括号,因此,如果您尝试使用进行访问,则JS将尝试调用AVG函数. notation, which doesn't work. 表示法,这是行不通的。 You'll need to use the bracket syntax to avoid the syntax problems. 您需要使用方括号语法来避免语法问题。

In the futute, I recommend using only alphanumeric (with at least one letter leading) characters only in keys. 在建议中,我建议仅在键中仅使用字母数字(至少以一个字母开头)字符。

This might help you :) 这可能对您有帮助:)

<script>
    var data = $.parseJSON('[{"data":{"AVG(Rate)":"7.5"}}]');
    alert(data[0]['data']['AVG(Rate)']);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM