torvictech
torvictech

Reputation: 59

jQuery parsing of server JSON (String)

I've been at this for a while and can't seem to nail it down, your help is much appreciated.

Challenge: Unable to identify how to properly use JQUERY to parse the following JSON (String?) response from the server:

{"d":"{\"NewDataSet\":{\"Table\":{\"EMPLOYEE_NO\":\"3605\",\"NAME\":\"Last, First\",\"STAFF\":\"CSR\",\"USERNAME\":\"lis\",\"PIN\":\"somepassword\"}}}"}

Tried: Many things, such as...

var dtObj = jQuery.parseJSON(data);
$.each(dtObj, function (i, val) {
.. do some stuff
});

Requirement: Need to get the EMPLOYEE_NO, NAME, STAFF, USERNAME, AND PING values.

Thanks!

Upvotes: 3

Views: 944

Answers (2)

Musa
Musa

Reputation: 97717

Your JSON id nested, so you'll have to parse twice and the object that holds the data you want is some levels deep.

var Obj1 = jQuery.parseJSON(data);
var Obj2 = jQuery.parseJSON(Obj1.d);
var dtObj = Obj2.NewDataSet.Table;
// now you can use dtObj to access EMPLOYEE_NO, NAME, STAFF, USERNAME, AND PING properties.

jsFiddle

Upvotes: 1

Tariqulazam
Tariqulazam

Reputation: 4585

First make sure, your json is valid. Use this tool http://jsonlint.com/

var data={
    "d": {
        "NewDataSet": {
            "Table": {
                "EMPLOYEE_NO": "3605",
                "NAME": "Last,First",
                "STAFF": "CSR",
                "USERNAME": "lis",
                "PIN": "somepassword"
            }
        }
    }
};
alert(data.d.NewDataSet.Table.NAME);

http://jsfiddle.net/tariqulazam/rBfLw/

Upvotes: 0

Related Questions