dimanche 30 novembre 2014

Move some JSON data into unique Divs


I have a blogger.com blog and I've managed to create a script that gets the data from the blog - im not very good at js / json so I going at this a little blind ;-)


Id like the output to be in 2 DIVS that are unique with the post ID and content:


This is the script:



<html>
<head>
<title>Test</title>
</head>
<body>

<script type="text/javascript">
function posts(json) {
// Get five recent posts
for (var i = 0; i < 2; i++)
{
var posturl;
// Get rel=alternate for truly post url
for (var j=0; j < json.feed.entry[i].link.length; j++)
{
if (json.feed.entry[i].link[j].rel == 'alternate')
{
posturl = json.feed.entry[i].link[j].href;
break;
}
}
// if the Blogger-feed is set to FULL, then the content is in the content-field
// if the Blogger-feed is set to SHORT, then the content is in the summary-field
if ("content" in json.feed.entry[i]) {
var postcontent = json.feed.entry[i].content.$t;}
else
if ("summary" in json.feed.entry[i]) {
var postcontent = json.feed.entry[i].summary.$t;}
else var postcontent = "";
// strip off all html-tags
var re = /<\S[^>]*>/g;
postcontent = postcontent.replace(re, "");
// reduce postcontent to 200 characters
if (postcontent.length > 200) postcontent = postcontent.substring(0,200);
// Get posts title
document.write("Post Id = "+json.feed.entry[i].id.$t);
document.write('<br/>');
document.write("Post content = "+postcontent);
document.write('<br/><br/>');
}
}
</script>

<script src="http://ironheartuk123.blogspot.com/feeds/posts/default?alt=json-in-script&callback=posts"></script>

</body>
</html>


When I load the page into a browser I get this on screen:



Post Id = tag:blogger.com,1999:blog-5655651846573938667.post-5882120439717312684
Post content = Test post 2

Post Id = tag:blogger.com,1999:blog-5655651846573938667.post-8794205203420774123
Post content = Test post 1


And this is what I'd prefer the output to be!



<div id="5882120439717312684">Test post 2</div>

<div id="8794205203420774123">Test post 1</div>


Once I have that step done I'll be able to move the Divs into the correct position on the page. I think that's the easy bit ;-)


Any help greatly appreciated!!





Aucun commentaire:

Enregistrer un commentaire