1

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try a different Syntax

var result = JSON.parse(data);

1 Comment

yes i think it might have been the name of the variable... thanks
0

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();

1 Comment

thank you. this also helped me. I think it was the name of the variable. I noticed you didn't use parseJson? This got me thinking when or why is it necessary to use parseJson?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.