I am making an ajax request to an external php file, then returning some json. Most of the json is straight forward but I can't seem to figure out how to parse the multidimensional part.
JSON:
{
"success":"TRUE",
"action":"JSON",
"date":"06/29/12",
"results":"true",
"numResults":2,
"0":[
"id":1234,
"name":"John Appleseed",
"gender":"male",
"average":2.5
],
"1":[
"id":5678,
"name":"Jessica Smith",
"gender":"female",
"average":1.4
]
}
jQuery:
$.ajax({
url: "searchController.php",
data: searchData,
type: "GET",
success:function(q) {
if (q) { // Results
$('#search .container .body .ajax .returnedHTML .loadingScreen').hide();
var json = $.parseJSON(q);
console.log(json);
if (json.success == "true") {
var numResults = json.numResults;
if (numResults == 1) {
$('#search .container .body .ajax .returnedHTML .content').contents().remove();
var htmlString = '<div class="searchContent"><ul><li class="returnedResults '+json.type+'"><a href="/#!/'+json.type+'/'+json.id+'/"><div class="title">'+json.name+'</div><div class="body"><div class="quickview"><ul><li><div class="average">'+json.average+'</div><br><span>Average</span></li><li><div class="rates">'+json.numrates+'</div><br><span>Rates</span></li><li><div class="followers">'+json.followers+'</div><br><span>Followers</span></li></ul></div></div></a></li></ul></div>';
$('#search .container .body .ajax .returnedHTML .content').append(htmlString);
console.log(htmlString);
}
}
});
How do I parse the JSON so I can access the all the data and not just the data in the first dimension? I have looked around on this site and I can't find anything that is either useful and/or helpful. Thank you so much!
3 Answers 3
Your arrays are at json[0] and json[1]
...
for(var i in json) {
if(!isNaN(i)) {
...
refer to you variables as json[i].name, json[i].average, etc.
...
}
}
Sign up to request clarification or add additional context in comments.
4 Comments
Joe Bowman
also
type doesn't exist anywhere in your example.jtorraca
Type is there, I just didn't copy all my JSON. Figured this was a good representation. I left out a few lines(on purpose). What would the variable isNaN be?Joe Bowman
the
isNaN() function checks to see whether the variable i is numeric (or not numeric, hence the !) - basically cycle all your keys [success, action, date, ... , 0, 1] check for thouse which are numeric (i.e. 0, 1, ... n) and treat as an array.Joe Bowman
And re:
type, I gathered the above was the case, hence 'in your example' :)$.ajax({
url: "searchController.php",
data: searchData,
type: "GET",
success:function(q) {
if (q) { // Results
$('#search .container .body .ajax .returnedHTML .loadingScreen').hide();
var json = $.parseJSON(q);
console.log(json);
if (json.success.toLowerCase() == "true") {
...
}
}
});
for(var key in json){
if(json.hasOwnProperty(key){
console.log(key);
console.log(json[key]);
}
}
answered Jun 29, 2012 at 7:20
user405398
Comments
About your JSON you have to pars same again,
var jsonArr1 = $.parseJSON(json.0);
var jsonArr2 = $.parseJSON(json.1);
answered Jun 29, 2012 at 7:10
Umesh Aawte
4,7007 gold badges44 silver badges52 bronze badges
1 Comment
Joe Bowman
shouldn't need to parse again, they'll parsed the first time round; they'll just be array within the original
json objectlang-js
qshould be the (already parsed) object. You don't have to parse the nested objects separately.