1

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!

2
  • 1
    Your JSON is invalid. Arrays (square brackets) don't have named properties. Assuming you fix this you don't need to parse it yourself, jQuery will do it for you before calling your callback. I.e., q should be the (already parsed) object. You don't have to parse the nested objects separately. Commented Jun 29, 2012 at 7:10
  • Good point; missed that - your [ ] around the array want to be { } to make it an Object. Commented Jun 29, 2012 at 7:21

3 Answers 3

2

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

also type doesn't exist anywhere in your example.
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?
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.
And re: type, I gathered the above was the case, hence 'in your example' :)
1
$.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]);
    }    
}

Comments

0

About your JSON you have to pars same again,

var jsonArr1 = $.parseJSON(json.0);
var jsonArr2 = $.parseJSON(json.1);

1 Comment

shouldn't need to parse again, they'll parsed the first time round; they'll just be array within the original json object

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.