Data Formats: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
= JSON = | = JSON = | ||
* JSON (JavaScript Object Notation) is the simplest and most commonly used. | * [http://en.wikipedia.org/wiki/Json JSON] (JavaScript Object Notation) is the simplest and most commonly used. | ||
* It is simply a JavaScript object turned into a string. | * It is simply a JavaScript object turned into a string. | ||
* It is the same as a BASIC object turned into a string. | * It is the same as a BASIC object turned into a string. | ||
Line 41: | Line 41: | ||
<div class="page-break"></div> | <div class="page-break"></div> | ||
= JSONP = | = JSONP = | ||
JSONP is a variation on JSON: "JSON with Padding". It wraps the JSON string in a function call. When your app receives the JSONP string, it executes it. | [http://en.wikipedia.org/wiki/JSONP JSONP] is a variation on JSON: "JSON with Padding". It wraps the JSON string in a function call. When your app receives the JSONP string, it executes it. | ||
<pre> | <pre> | ||
Line 83: | Line 84: | ||
= XML = | = XML = | ||
* XML is the X in AJAX. | * [http://en.wikipedia.org/wiki/Xml XML] is the X in AJAX. | ||
* More verbose than JSON - more data to download | * More verbose than JSON - more data to download | ||
* Works just as well | * Works just as well |
Latest revision as of 16:12, 11 December 2014
There are several formats commonly used to transfer data to and from Web Services.
JSON
- JSON (JavaScript Object Notation) is the simplest and most commonly used.
- It is simply a JavaScript object turned into a string.
- It is the same as a BASIC object turned into a string.
Here is what it looks like in expanded form:
{ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" }, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }
AppStudio has a couple of handy statements for manipulating JSON strings:
myObject = JSON.Parse(jsonString) 'Convert a JSON string to an object jsonString = JSON.Stringify(myObject) 'Convert an object into a JSON string
JSONP
JSONP is a variation on JSON: "JSON with Padding". It wraps the JSON string in a function call. When your app receives the JSONP string, it executes it.
functionCall({ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" }, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] } )
The above JSON string will call a function in your program called functionCall with the string as a parameter:
Sub functionCall(customer) Print customer.lastName 'Will print "Smith" End Sub
See the AjaxStockQuote for an example of this.
XML
- XML is the X in AJAX.
- More verbose than JSON - more data to download
- Works just as well
<pre> <person> <firstName>John</firstName> <lastName>Smith</lastName> <age>25</age> <address> <streetAddress>21 2nd Street</streetAddress> <city>New York</city> <state>NY</state> <postalCode>10021</postalCode> </address> <phoneNumbers> <phoneNumber type="home">212 555-1234</phoneNumber> <phoneNumber type="fax">646 555-4567</phoneNumber> </phoneNumbers> </person> <person firstName="John" lastName="Smith" age="25"> <address streetAddress="21 2nd Street" city="New York" state="NY" postalCode="10021" /> <phoneNumbers> <phoneNumber type="home" number="212 555-1234"/> <phoneNumber type="fax" number="646 555-4567"/> </phoneNumbers> </person>
- Need to include the xml2json library to convert XML into an object
data=$.xml2json(req.responseText)