Data Formats

From NSB App Studio
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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)