HUNG
HUNG

Reputation: 535

How to get the value from this json string?

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

How can I alert 7.5? I have tried data[0].data.AVG(Rate), data.data.AVG(Rate), data.AVG(Rate)

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

Upvotes: 0

Views: 53

Answers (2)

Rohit Subedi
Rohit Subedi

Reputation: 550

This might help you :)

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

Upvotes: 0

beatgammit
beatgammit

Reputation: 20205

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)

This has parenthesis, so JS will try to call the AVG function if you try to access it with . 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.

Upvotes: 3

Related Questions