I’m exporting my php as jsonp and then calling a js file which loads in the data.
I’m getting the output of only the last data object. I would like to loop through the action data and have all of them output into the <li>data.action</li> format. If anyone can help figure how to do this thank you so much.
JSONP
({
"posts": [{
"action": "go to acting school",
"action": "go to a beach",
"action": "go kyacking",
"action": "go skiing in vermont",
"action": "sleep in a cave",
"action": "dress up as favorite celebrity in public",
"action": "see the cherry blossums in Washington DC",
"action": "watch all the rocky movies",
"action": "go sky diving",
"action": "climb Mount Everest",
"action": "climb K2",
"action": "see or participate in war reenactment",
"action": "spend New Years Eve in Times Square",
"action": "jump out of a moving plane",
"action": "jump out of a moving car",
"action": "try eating a new food"
}]
})
JQUERY
$(document).ready(function () {
$.getJSON("http://site.com/lesson.php?count=5&jsoncall=?", function (data) {
$.each(data.posts, function (i, data) {
var jsondata = "<li>" + data.action + "</li>";
$(jsondata).appendTo("ol#results");
});
});
return false;
});
That isn’t proper json. When converted to a javascript object, it will contain:
This is what i believe it should be instead:
Obviously this is with no knowledge of what the json actually represents. The thinking behind the change i made is if they are all actions, place them all in one array that is stored in a property called
actionsYou would then output it with: