2011-06-25 4 views
4

je veux enregistrer chaque réponse JSON dans un tableau que j'ai écrit comme cecifaçon d'économiser de réponse JSON pour tableau

$(document).ready(function() { 
    var saveJson = []; 
    var jsonResponse = getjsonResponse() 
         //something with $get function response json format 
    saveJson = saveToArray(jsonResponse.SearchResults); 

    var saveToArray = function(array){ 
     $.each(array,function(i,data){ 
      saveJson.push(data); 
     }); 
    }; 
}); 

mais semble être mon code ne fonctionne pas saveJson obtenir non défini comment puis-je surmonter cette ? simplement je veux ajouter la réponse de JSON à un objet de tableau.

regarder réponse mon exemple comme

"SearchResults":[ 
      { 
      "TypeFlag":"W", 
      "HashCode":"66093013", 
      "ShortenKey":"http:///k", 
      "Title":"Yahoo! \u003cspan class=\"search-result-highlight\"\u003eSearch\u003c/span\u003e - Web \u003cspan class=\"search-result-highlight\"\u003eSearch\u003c/span\u003e", 
      "Description":"The \u003cb\u003esearch\u003c/b\u003e engine that helps you find exactly what you\u0027re looking for. Find the most relevant information, video, images, and answers from all across the Web.", 
      "Url":"http://search.yahoo.com/", 
      "LastUpdateOn":"6/21/2011 1:01:11 PM", 
      "PageRank":1, 
      "ThumbImageUrl":"" 
      }, 
     { 
      "TypeFlag":"W", 
      "HashCode":"48394089", 
      "ShortenKey":"http:///5i", 
      "Title":"Lijit | Advertising Services, Audience Analytics and Publisher ...", 
      "Description":"I’ve been using Lijit as my site \u003cb\u003esearch\u003c/b\u003e for several years and the understanding I get about my audience is critical to knowing what my readership is interested in and ...", 
      "Url":"http://www.lijit.com/", 
      "LastUpdateOn":"6/22/2011 11:31:41 PM", 
      "PageRank":10, 
      "ThumbImageUrl":"" 
     } 
] 

grâce

+0

Votre code ne fait pas vraiment de sens. Vous affectez une fonction 'jsonResponse', puis passez à' saveToArray', en traitant la fonction comme un tableau. Si vous appelez réellement la fonction a pour obtenir la réponse, je suppose que vous faites une requête Ajax. Ajax est ** asynchrone **. Vous devez utiliser un rappel qui gère la réponse. Je vous suggère de jeter un coup d'oeil à quelques exemples d'Ajax. –

+0

quel est votre échantillon json? – naveen

+0

OK, je vais modifier avec mon objet exemple json – Gayan

Répondre

2
function(array){ 
    $.each(array,function(i,data){ 
     saveJson.push(data); 
    }); 
}; 

renvoie undefined. Bien que vous chargiez des données dans saveJson, votre "saveJson = saveToArray (jsonResponse)" réattribue le saveJson comme undefined.

Vous pourriez avoir:

function(array){ 
    var ret = []; 
    $.each(array,function(i,data){ 
     ret.push(data); 
    }); 
    return ret; 
}; 
+0

j'ai accepté ceci mais pas la meilleure réponse à ma question. – Gayan

0

Votre JSON est pas un tableau, vous avez fait un objet contenant un tableau dans « SearchResults », donc vous devez passer ce lieu de la totalité de l'objet:

saveJson = saveToArray(jsonResponse.SeachResults); //change this line 

(en supposant jsonResponse est défini)