Chapter 19. Working with Structured Data
19.0. Introduction
Even though it’s primarily a client-side development tool, JavaScript has access to several very sophisticated techniques and mechanisms for working with structured data.
Originally, most complex data management, particularly in relation
to Ajax calls, was based either in HTML fragments or XML. HTML fragments
are still popular. When returned from an Ajax request, the HTML formatted
string can be used with innerHTML in
order to easily append the HTML into the page. Of course, you have to
trust the data you receive, and you can’t manipulate it until you actually
insert the data into the page. Either that, or use various String functions to parse pieces out of the HTML
string.
Data returned as XML is a richer option, because you can use the string to create a separate document object, from which you can access individual elements, just like you query the web page document now.
XML data is still widely supported, but another structure has gained a great deal of popularity: JavaScript Object Notation, or JSON. It’s so popular that JSON has been added to the most recent version of JavaScript, ECMAScript 5. JSON provides the richness of XML but without the performance hit client-side processing of XML can add.
JSON is basically the string serialization of JavaScript objects.
Before ECMAScript 5, you had to use the eval function to convert the string to an object
. Unfortunately, using the eval function on an unknown string is an application-security ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access