Consuming and sending data from a JSON web service.

1. Introduction

JSON (JavaScript Object Notation) is a light template for storing and transmitting information in text format. Although very simple, this template has been used in several web applications; this is because it can structure information more compactly than XML templates.

In JSON, data are presented like this:

An object is a disordered pair comprising name/value; it starts with an { (opening curly bracket) and ends with an } (ending curly bracket). Each name is followed by a : (colon), and name/value pairs are followed by a , (comma).

An array is a collection of ordered values; it starts with an [ (opening square bracket) and ends with an ] (ending square bracket). Values are separated by a  , (comma).

Example:

[
    {
        “id”: 1,
        “username”: “ElipseRS”,
        “password”: “Eli123”
    },
    {
        “id”: 2,
        “username”: “ElipseSP”,
        “password”: “EliSP123”
    },
    {
        “id”: 3,
        “username”: “ElipseMG”,
        “password”: “ElipMG123”
    }
]

This article illustrates how to consume and send data via Elipse E3 to a web service developed in JSON format.

2. JSON web service

We developed a JSON web service for simulating values. In order to confirm the HTTP requirement data, the sent values and a success message (in JSON format) were created at POST, and the values returned according to the pattern established by the protocol were created at GET.

The web service has the following format:

[
    {“id”:1,”email”:”scherer@elipse.com.br”,”username”:”scherer”,”password”:”password”},         
    {“id”:2,”email”:”delio@elipse.com.br”,”username”:”delio”,”password”:”password123″},  
    {“id”:3,”email”:”enrico@elipse.com.br”,”username”:”enrico”,”password”:”password321″}
]

3. Integrating Elipse E3 with JSON web service

Similarly to XML, VBScript does not support JSON natively. To work around this, you can use a VBScript class that parses a string in JSON (which starts with a “{” or a “[“), or that conversely calls HTTP with the “msxml2.ServerXMLHTTP” component and evaluates the JSON string returned by this call.

NOTE: The string used for creating the Microsoft® XML Core Services (MSXML) component may vary according to the version available in the system. You can check for the available “msxml” DLLs at C:\Windows\system32.


4. Consuming data from the web service (GET)

The script below was used for reading data from the web service:

Sub CommandButton1_Click()
Dim oJsonParser
Set oJsonParser = new aspJSON   ‘Creates aspJSON object‘Webservice
URL = “http://www.filltext.com/?rows=3&id={index}&email={email}&username={username}&password={randomString|5}&pretty=true”
oJsonParser.LoadJSON(URL)
For i = 0 to oJsonParser.data.count – 1
   Screen.Item(“ID”&i&“”).Value = oJsonParser.data(i).item(“id”)
   Screen.Item(“Email”&i&“”).Value = oJsonParser.data(i).item(“email”)
   Screen.Item(“User”&i&“”).Value = oJsonParser.data(i).item(“username”
   Screen.Item(“Password”&i&“”).Value = oJsonParser.data(i).item(“password”)
Next
End Sub

 

NOTE: The aspJSON class that was referenced above was not displayed, but it is the responsible for parsing a JSON string. This class is available at the attached demo application, though.

5. Writing in the web service (POST)

To write on the web service, we used ServerXMLHTTP object, which provides methods and properties that allow you to establish an HTTP connection between files or objects in different web servers. The script below was used for writing data on the web service:

Sub CommandButton2_Click()
Dim objXmlHttpMain , URL
strJSONToSend = “{“”id””: “”0″”, “”email””: “”elipse@elipse.com.br””, “”username””: “”Elipse””, “”password””: “”Elipse123″”}”
URL= “http://scherer:5000/users/” ‘Webservice

Set objXmlHttpMain = CreateObject(“Msxml2.ServerXMLHTTP”)
On Error Resume Next
objXmlHttpMain.open “POST”,URL, False
If Err Then            ‘handle errors
  Msgbox Err.Description & ” [0x” & Hex(Err.Number) & “]
End If
On Error Goto 0
objXmlHttpMain.setRequestHeader “Content-Type”, “application/json”
objXmlHttpMain.send strJSONToSend
strResponse = objXmlHttpMain.responseTextmsgbox strResponse
End Sub

 


6. Demo application

The demo application below was developed with Elipse E3’s version 4.7 build 252 and projected to be compatible with the structure of the web service illustrated in item 2. Other formats will require customization, therefore the web service’s URL address must be edited in the script.

 

Attachments:

Class_aspJSON.zip
App.zip
JSON_VBScriptDriver.zip

Este artigo foi útil? Was this post helpful?
Yes0
No0

Leave a Reply

Your email address will not be published.Required fields are marked *