0

I am making an ajax call that returns some json data. I need to take this data, loop through it and build a new javascript array.

This is what the returned json looks like:

{
    query: [ ],
    products: 
[
    
{
    title: "title 1",
    price: "6.00",
    magazine: "magazine name 1",
    image: "/p/c/pc_90_cover.jpg",
    type: "Magazine",
    market: "Technology",
    zinio: "http:www.zinio.com",
    newsstand: "http://www.link1.php"
    },

    
{
    title: "title 2",
    price: "6.00",
    magazine: "magazine name 2",
    image: "/p/c/pc_90_cover.jpg",
    type: "Magazine",
    market: "Technology",
    zinio: "http:www.zinio.com",
    newsstand: "http://www.link2.php"
    },

    
{
    title: "title 3",
    price: "6.00",
    magazine: "magazine name 3",
    image: "/p/c/pc_90_cover.jpg",
    type: "Magazine",
    market: "Technology",
    zinio: "http:www.zinio.com",
    newsstand: "http://www.link3.php"
    }
    ]

}

How do I loop through this data in javascript? This is what I have so far but it is very wrong! - apologies my javascript is not my strongest skill!

                    var allProducts = $.get("http://localhost:8888/imagine-publishing/web/app_dev.php/api/v1/search/search.json?query=pho", function(data) {


                    var arrProducts = [
                        for (var product in allProducts) {

                            title  = product.title,
                            url = product.url, 
                            label = product.title, 
                            magazine = product.magazine,
                            image = product.imageThumb,
                            newsstand = product.newsstand,
                            googleplay = product.googleplay,
                            kindle = product.kindle, 
                            barnesnoble = product.barnesnoble,
                            zinio = product.zinio, 
                            kobo = product.kobo, 
                            waterstones = product.waterstones, 
                            type = product.type, 
                            brandurl = product.brandurl 

                            },

                     ];

                });

                console.log(arrProducts);

3 Answers 3

4

Assuming the JSON is served with the correct Content-Type (application/json), jQuery will automatically parse the JSON and populate the first argument of the callback function with the result.

var arrProducts = data.products;
Sign up to request clarification or add additional context in comments.

1 Comment

When I console.log the arrProducts it returns an array of objects. The data needs to be in the exact format as the example above. Is it possible to do this using your method?
1

http://api.jquery.com/jQuery.parseJSON/

jQuery.parseJSON("json string")

Comments

0

Using jQuery's getJSON

http://api.jquery.com/jQuery.getJSON/

  $.getJSON(url, function(data){
   // Your code goes here
  });

1 Comment

Beaten by minutes, I need to type quicker

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.