Richie
Richie

Reputation: 5197

jquery parseJson returning null

I'm trying to parse the following json using jquery...

{
    "notificationhistory": [
        {
            "userid": "Richard",
            "createdtime": "2014-10-01T15:20:55",
            "actiontype": "Y",
            "note": "Richard test",
            "actioncode": "AC",
            "lastmodified": "2015-04-28T10:52:28"
        }
    ]
}

My jquery function to try and do this looks liek this....

function loadNotificationBarData() {
    var url = "json/notificationBar.action";
    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'json',
        success: function(data) {
            var json = $.parseJSON(data);               
            alert(json.notificationhistory[0].actiontype);
            alert(json.notificationhistory[0].actioncode);
            alert(json.notificationhistory[0].note);               
        }
    });
}

But this is not working for me. I keep getting a null for my var json.

Can someone help me with this please?

thanks

Upvotes: 1

Views: 86

Answers (3)

ashkufaraz
ashkufaraz

Reputation: 5307

this worked Online Demo

function loadNotificationBarData() {
    var data = {
        "notificationhistory": [
            {
                "userid": "Richard",
                "createdtime": "2014-10-01T15:20:55",
                "actiontype": "Y",
                "note": "Richard test",
                "actioncode": "AC",
                "lastmodified": "2015-04-28T10:52:28"
            }
        ]
    };              
    alert(data.notificationhistory[0].actiontype);
    alert(data.notificationhistory[0].actioncode);
    alert(data.notificationhistory[0].note);    
}

loadNotificationBarData();

Upvotes: 0

tonesforchris
tonesforchris

Reputation: 303

Unless I'm mistaken, jQuery $.ajax already parses the data to json automatically for you, so just remove the var json = $.parseJSON(data); code as its not needed

Upvotes: 1

Brad Baskin
Brad Baskin

Reputation: 1217

Try a different Syntax

var result = JSON.parse(data);

Upvotes: 1

Related Questions