2

i want to save each json response in an array i did write like this

$(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);
       });
   };
});

but seems to be my code does not work saveJson getting undefined how can i overcome this? simply i want to append json response to one array object.

my sample response look like

    "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":""
      }
]

thanks

4
  • Your code does not really make sense. You assign a function jsonResponse and then pass it to saveToArray, treating the function as array. If you are actually calling the a function to get the response, I assume you make an Ajax request. Ajax is asynchronous. You have to use a callback that handles the response. I suggest you have a look at some Ajax examples. Commented Jun 25, 2011 at 12:50
  • OK i'll edit with my json sample object Commented Jun 25, 2011 at 12:53
  • 1
    What does getjsonResponse do? If ithas an async request, you need a callback function to process it. (not just assume it's loaded on the next line) Commented Jun 25, 2011 at 12:54
  • @digitalFresh it's jquery $get function getting json format response Commented Jun 25, 2011 at 12:59

2 Answers 2

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

returns undefined. Although you push data into saveJson, your "saveJson = saveToArray(jsonResponse)" re-assign the saveJson as undefined.

You might have :

function(array){
    var ret = [];
    $.each(array,function(i,data){
        ret.push(data);
    });
    return ret;
};
Sign up to request clarification or add additional context in comments.

Comments

0

Your JSON is not an array, you actually have an object containing an array in "SearchResults", so you have to pass that on instead of the entire object:

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

(assuming jsonResponse is defined)

Comments

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.