Several people asked me how to implement a custom JSON Service Broker for K2. There are a few examples out there, but this is a quick reference that will get you up and running.
The first step is to create a custom K2 Service Broker. To create a basic service broker, follow the steps in the previous blog post – “How to create a custom service broker”
K2 Automated Testing Software
BenchQA allows full test automation of K2, including fully automated K2 SmartForms and K2 Workflow testing. It promotes test driven development for K2 and ensures continued quality assurance for K2 solutions. Easily apply changes to test cases to accommodate changes to K2 apps and ensure all apps are regression tested to avoid defects and assure continuous quality.
Then, follow the steps below to implement the K2 JSON Service Broker that allows us to read JSON data and display it in a K2 SmartForm.
Step 1: Create a List Method.
After you created your custom K2 Service Broker as described in the blog post mentioned above, implement your List method:
Step 2: Create the request to the URL.
Here we send the request to the web service to get the JSON data back.
The URL would be the web service URL that contains the JSON data. After getting the JSON data back, we need to deserialize it into an object.
Step 3: Deserialize the JSON object into a Root Class.
Because we know how the object looks like – we created a class for the JSON data- we can handle the data better and increase the performance of the List method.
Step 4: Iterate through the JSON list data.
After deserialising the object, iterate through the RootContract.Contracts and set the JSON data to the K2 Service Broker’s properties.
Here is a full example of the code when we deserialize the object and iterating through them.