Solved

JSON and Prototype

Posted on 2011-02-14
9
224 Views
Last Modified: 2013-09-30
please help me use prototype to populate the 'title' div with the data inside this JSON.
      
JSON:
{
    "blogPosts" : [
        {
"postTitle" : "My Blog post title",
            "content" : "my main content",
            "image" : "th-4.jpg",
            }
    ]
}

html: <div id="title"></div>

Thanks!
0
Comment
Question by:ram0nm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34890312
document.getElementById("title").innerHTML = JSON.blogPosts.postTitle;

check this also
http://api.jquery.com/jQuery.getJSON/
0
 

Author Comment

by:ram0nm
ID: 34890379
That didn't work. btw I needed this to be done using prototype not jQuery.

I think in prototype it should look like this, but it's still not working, not sure what I'm doing wrong:
$('title').innerHTML = JSON.blogPosts.postTitle; 

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34890394
try it

document.getElementById("title").innerHTML = eval(JSON).blogPosts.postTitle;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:gurvinder372
ID: 34890408
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34903847
The path to get the title is :
myJson["blogPosts"][0].postTitle
or :
myJson.blogPosts[0].postTitle

To set the innerHTML you should use update : http://www.prototypejs.org/api/element/update
$('title').update(json.blogPosts.postTitle);

Open in new window


Test page :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
<script language="javascript">
	var myJson = {"blogPosts":[{"postTitle":"My Blog post title","content":"my main content","image":"th-4.jpg"}]};
	Event.observe(window, 'load', function() {
		$('title').update(myJson["blogPosts"][0].postTitle);
	});
</script>
</head>
<body>
<div id="title"></div>
</body>
</html>

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39495092
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39495093
My last comment fully answered the question :
- use prototype instead jQuey
- show the path to the data
- working example provided
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question