Data Formats: Difference between revisions
Jump to navigation
Jump to search
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
There are several formats commonly used to transfer data to and from Web Services. | |||
= JSON = | = JSON = | ||
* [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 the same as a BASIC object turned into a string. | |||
Here is what it looks like in expanded form: | |||
<pre> | <pre> | ||
{ | { | ||
Line 23: | Line 31: | ||
} | } | ||
</pre> | </pre> | ||
AppStudio has a couple of handy statements for manipulating JSON strings: | |||
<pre> | |||
myObject = JSON.Parse(jsonString) 'Convert a JSON string to an object | |||
jsonString = JSON.Stringify(myObject) 'Convert an object into a JSON string | |||
</pre> | |||
<div class="page-break"></div> | |||
= JSONP = | = JSONP = | ||
[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> | ||
functionCall({" | 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" | |||
} | |||
] | |||
} | |||
) | |||
</pre> | </pre> | ||
The above JSON string will call a function in your program called functionCall with the string as a parameter: | |||
<pre> | |||
Sub functionCall(customer) | |||
Print customer.lastName 'Will print "Smith" | |||
End Sub | |||
</pre> | |||
See the AjaxStockQuote for an example of this. | |||
<div class="page-break"></div> | |||
= XML = | = XML = | ||
* [http://en.wikipedia.org/wiki/Xml XML] is the X in AJAX. | |||
* More verbose than JSON - more data to download | |||
* Works just as well | |||
<pre> | <pre> | ||
<pre> | <pre> | ||
Line 54: | Line 112: | ||
</phoneNumbers> | </phoneNumbers> | ||
</person> | </person> | ||
</pre> | |||
* Need to include the xml2json library to convert XML into an object | |||
<pre> | |||
data=$.xml2json(req.responseText) | |||
</pre> | </pre> |
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)