Article Details
Id:13061
Product:finPOWER Connect
Type:NEW
Version:2.03.00
Opened:29/06/2015
Closed:01/07/2015
Released:09/07/2015
Job: J015797

Business Layer; added JSON Utilities and Token classes for parsing JSON strings

Two new classes have been added to the finPOWER Connect Business Layer to make it easy to parse JSON strings.

These classes are found in the Runtime namespace. For more information see the finPOWER Connect Business Layer help and Custom Web Services Guide. A code example is shown below:

Public Function Main(ByVal parameters As ISKeyValueList) As Boolean

  Dim i As Integer
  Dim JsonText As String
  Dim JsonToken As ISJsonToken
  Dim JsonTokenRoot As ISJsonToken
  Dim JsonTokens() As ISJsonToken

  ' Assume Success
  Main = True

  JsonText = "{""AccountId"":""L1000"",""Payments"":[{""Date"":""2015-05-01T00:00:00"",""Reference"":""AP"",""Value"":120.0},{""Date"":""2015-06-01T00:00:00"",""Reference"":""DD"",""Value"":125.0}]}"

  If finBL.Runtime.JsonUtilities.LoadJsonTokenFromString(JsonText, JsonTokenRoot) Then
    finBL.DebugPrint(String.Format("AccountId : {0}", JsonTokenRoot.GetPropertyString("AccountId")))

    JsonTokens = JsonTokenRoot.GetPropertyArray("Payments")
    For i = 0 To JsonTokens.Length - 1
      JsonToken = JsonTokens(i)

      finBL.DebugPrint("")
      finBL.DebugPrint(String.Format("Item {0}", i))
      finBL.DebugPrint(String.Format("Date : {0}", finBL.FormatDateLong(JsonToken.GetPropertyDate("Date"))))
      finBL.DebugPrint(String.Format("Reference : {0}", JsonToken.GetPropertyString("Reference")))
      finBL.DebugPrint(String.Format("Value : {0}", JsonToken.GetPropertyDecimal("Value")))
    Next
  Else
    Main = False
  End If
  
End Function

Outputs the following debug text:

AccountId : L1000

Item 0 Date : 01/05/2015 Reference : AP Value : 120

Item 1 Date : 01/06/2015 Reference : DD Value : 125