Juliver Galleto
Juliver Galleto

Reputation: 9037

json post retrieved data could not fetch

I have this json script and I successfully retrieved the data that consist of 3 arrays of json object (console.log)

 $.post("/sales", {dataKey: "pullout"}, function(response){
    if(response.success){
        $.each(response.portfolio, function(index, value){
            alert("value.creation_date");
        });
        $.each(response.var, function(index, value){
            alert("value.creation_date");
        });
        $.each(response.par, function(index, value){
            alert("value.creation_date");
        });
    }
 }, 'json');

and I got this result from the "console.log(response)" enter image description here

either using "console.log(response.portfolio)", "console.log(response.var)", or "console.log(response.par)", I get a result of the data that I wanted to retrieved. But if I want to parse it with this (refer below)

$.each(response.portfolio, function(index, value){
    alert("value.creation_date");
});
$.each(response.var, function(index, value){
    alert("value.creation_date");
});
$.each(response.par, function(index, value){
    alert("value.creation_date");
});

it returns me this (refer below) enter image description here

so obviously, what I want to do (base from the referenced script above) I want to loop through each of the array objects of portfolio array, par array and var array and get each "creation_date" object but unfortunately, its not working. Any ideas, clues, help, suggestion, recommendation?

Upvotes: 0

Views: 57

Answers (2)

Rey Libutan
Rey Libutan

Reputation: 5314

Your portfolio, par and var are still strings.

Get the object using JSON.parse().

Your code becomes:

var portfolio = JSON.parse(response.portfolio);
$.each(portfolio, function(index, value){
    alert(value.creation_date);
});

Upvotes: 1

user2218824
user2218824

Reputation:

As @incognito already said the values of response.portfolio, response.par and response.var are strings. What you have to do is to parse those strings as JSON:

$.each(JSON.parse(response.portfolio), function(index, value){
    alert("value.creation_date");
});

Upvotes: 1

Related Questions