500 likes | 1.14k Views
JSON vs XML. Justin Zintak. Background. This project is a continuation of a project that I have been working on for a few months . It is an website for managing your online reputation called Repio . www.repio.com. Background.
E N D
JSON vs XML Justin Zintak
Background • This project is a continuation of a project that I have been working on for a few months. • It is an website for managing your online reputation called Repio. • www.repio.com
Background • We are currently using the Yahoo search API, which returns values in a JSON format. • In this case, the text is not “xml” in itself, but the process is the same; make a request, read the response, and parse the values.
Purpose • For the purpose of this project I wanted to see if I could do the same thing, but with XML. • Throughout thecourse of my projectI noted some differences I found betweenJSON and XML. • There has been a lot of previous work distinguishing between XML and JSON.
JSON • JSON, or JavaScript Object Notation, is a lightweight data-interchange format. • It is based on a subset of the JavaScript Programming Language. • JSON is built on two structures: • A collection of name/value pairs. In various languages, this is realized as an object, record, hash table, keyed list, or array. • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
JSON vs XML • Primarily, I noticed that JSON and XML are really built for different purposes (obviously, but I got to see the differences first hand…). • XML is built for giving semantic meaning to text within documents. • JSON is built for data structures. • XML can be used to describe data structures, but when you need to deal with syntax for describing simple things like empty arrays, JSONis simply easier to work with.
JSON vs XML • A strict JSON parser is not a lot of code. • I was able to write code returning JSON values in less than 10 lines (in CF and Java). • XML has a large set of extraction and transformation tools. • Stream and DOM (Document Object Model) parsers are widely available.
Code for a JSON Response in CF jsonString is just a formatted string. “deserializeJson” converts a JSON string into CF data, such as a CF structure or array. item is set to the result of this process on the formatted jsonString. Loop through and save each of the results of the search.
Code for a JSON Response in Java The String, jsonResponse is just a formatted string. There is a tool called “Gson()” that is able to convert the formatted string into a Java Structure. The YahooBossResult.class is a Java object that has properties that correspond to what is returnedby JSON. In this case, JSON is returning a deserialized version of the jsonResponse string into an object of type YahooBossResult.class Next, I loop through the results of the retrieved values. For each YahooBossRow called row, get the response and results. Then do something with the values. getResponsecode() == 200 is an HTTP Status-Code meaning everything is ‘ok’ As long as the structure of the class fits, the code should execute. If it doesn’t fit, it will throw a parse exception.
Code for an XML Response This basically readies the search for the current user, in this case Steve Jobs. This is the get method for searching with the given parameters.
Code for an XML Response Adds the results to the query to be displayed. Outputs and saves the results.
Code for an XML Response XMLParse converts XML text into an XML document object. Loops through the search results (limited to 50), and sorts the data into a link, title, data, a description…etc
Code for an XML Response Output and save the data
Demo • I have the CF JSON code working and producing results.