PDA

View Full Version : Urgent: how to use ajax with json



cutexxbaby
19 May 2011, 10:57 AM
hi
i am currently do a project
i need to use an json but i am ask to put the json format with the data into a dummy html page
how am i going to do that?
cause i need it to use later with the ajax, so i take the html url and put it in
however i need to only retrieve two attribute...so how to embedded the json in the html so that i can use it in ajax to retrieve two attribute (eg title and publisher)

["book":

{
"title":"JavaScript, the Definitive Guide",
"publisher":"O'Reilly",
"author":"David Flanagan",
"cover":"/images/cover_defguide.jpg",
"blurb":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
},


{
"title":"DOM Scripting",
"publisher":"Friends of Ed",
"author":"Jeremy Keith",
"cover":"/images/cover_domscripting.jpg",
"blurb":"Praesent et diam a ligula facilisis venenatis."
},

{
"title":"DHTML Utopia: Modern Web Design using JavaScript & DOM",
"publisher":"Sitepoint",
"author":"Stuart Langridge",
"cover":"/images/cover_utopia.jpg",
"blurb":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."

}
]

cyberjef
22 May 2011, 12:35 AM
What I do is just write the json data to a .js file and link it in the page you need it in.

Note that you will have to assign that data to a variable:
Your JSON data doesn't look right to me though, but I don't really know.

Ex:

<script type="text/javascript">

var J = {"book":{"title":"JavaScript, the Definitive Guide","publisher":"O'Reilly","author":"David Flanagan","cover":"/images/cover_defguide.jpg","blurb":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."}};


alert(J['book']['title']);

</script>