Skip to content

Instantly share code, notes, and snippets.

@thurloat
Created February 4, 2011 02:25
Show Gist options
  • Save thurloat/810642 to your computer and use it in GitHub Desktop.
Save thurloat/810642 to your computer and use it in GitHub Desktop.
2 Options for parsing some JSON data.
/*
* Long and Dreary.
*/
var json = xml2json.parser(response_text),
query_result = json.envelope.body.queryresponse.result,
users = [];
// Build up a list of {{sd.User}}s to return to the view.
if(query_result.size == 1){
// Only a single user returned from the search
users.push(new sd.User(query_result.records));
} else if(query_result.size > 0){
for (var query_i=0; query_i < query_result.records.length; query_i++) {
users.push(new sd.User(query_result.records[query_i]));
};
}
return users
/*
* Short and Sweet.
*/
var json = xml2json.parser(response_text),
query_result = json.envelope.body.queryresponse.result,
records = [].concat(query_result.records),
users = [];
// Build up a list of {{sd.User}}s to return to the view.
for (var query_i=0; query_i < records.length; query_i++) {
users.push(new sd.User(records[query_i]));
};
return users;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment