how to put json data within HTML

hi i want to know how to put the json data into html is there a tag or what and is it put under body?

`["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."

    }
]`
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

    var addrField = document.getElementById('myDiv');

    var card = eval('(' + xmlhttp.responseText + ')');
    addrField.value = card.places.place.name.value;
    }
  };

xmlhttp.open("GET","HTMLPage3.htm",true);
xmlhttp.send();

Answers


What you have is not valid JSON. The book property is not correctly defined. You could use the <script> tag to store this in your HTML document and ensure you have fixed this JSON or you will probably get javascript errors:

<script type="text/javascript">
var books = [
    {
        "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."
    }
];
</script>

Now you can loop through the elements of this array:

for (var i = 0; i < books.length; i++) {
    var book = books[i];
    alert(book.title);
}

Need Your Help

Error on a percentile star graph (probably syntax)

javascript html canvas

I have edited three different NotePad documents for a quiz that will be used in the upcoming school year. The main use for this is he tester answers radio buttons that come out as percentages at th...

Deserialising XML based on attribute values

c# .net xml xml-serialization

I have some XML that I need to deserialise

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.