I'm trying to parse out some data thats returned from a web service via json with the following code.
function getADData() {
var strSearch = $('#txtSearch').val()
var ajaxData = "{ 'PartNameString': '" + strSearch + "' }";
$.ajax({
type: "POST",
url: "/Services/ActiveDirectoryInterop.asmx/SearchUsers",
data: ajaxData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success:populateTable
});
}
function populateTable(result) {
alert(result["d"].length);
}
Data Returned is this.
{"d":{"Columns":["UserID","Name","Email"],"Rows":[["U99999","Lees, Smith","someemail1@canfor.com"],["U99999","Lees, Mark","someemail1@canforpulp.com"],["99999","Lees, Bob","someemail1@canforpulp.com"],["U999999","Lees, John","someemail1@canforpulp.com"],["U999999","Lees, Jim","someemail1@canforpulp.com"]]}}
What the alert though jsut returns undefined. So i know I'm missing something and it probably has to do with the nesting of the JSON. Can someone point me in the right direciton for some materials or code that shows me how to possibly traverse data like what i'm recieving.
You're interested in length of which property? Columns or rows? Anyway these two lines show each of them.
alert(result.d.Columns.length);
alert(result.d.Rows.length);
As you can see I'm accessing properties with accessor operator instead of indexer. But you could as well do the same this way:
alert(result["d"]["Columns"].length);
alert(result["d"]["Rows"].length);
But I find the first approach much cleaner because I know exactly I'm working with a Javascript object and not an array (Javascript object is actually an associative array but let's not mind these details).
Are you looking for the number of rows? This should do it:
alert(result.d.Rows.length);