Modeling responses

What we need to do with that JSON blob is to generate C# classes from it so that the response can be accessed in Unity. The simplest way to go about this, even before creating our HTTP operation, is to get a sample of the response from tools such as the APIGEE Console(defunct) or use Postman for your own private APIs.

  • Step 1: Get the JSON Blob

Go to the APIGEE Console for Instagram(defunct) and select OAuth2 from the Authentication dropdown. Then follow the prompts to allow APIGEE access to your Instagram account. At this point the Authentication dropdown should say ‘instagram-AuthenticatedUser’. From the API Method selection pick the method we have been working with: ‘tags/{tag-name}/media/recent’. Directly in the Request URL you can replace ‘{tag-name}’ with ‘games’ and press Send. Once a response arrives, click the Snapshot button. At the Snapshot screen, click the View Raw link and copy all of the JSON data into the clipboard.

  • Step 2: Generate C# Classes

With the raw JSON text in our clipboard go to JSONUtils.com. Clear out the ‘JSON Text or URL’ field and paste in your JSON text. Click Submit. You just generated a C# representation of the JSON data!

Copy the C# code found in the lower pane and paste it below your HTTP operation. Notice that the generated class named ‘Example’ is the root class of the response.

NOTE: Replace all instances of IList to either List or an array.

Step 3: Update Operation

Let’s modify our operation a little and add the following class field.

[hg.ApiWebKit.mappers.HttpResponseJsonBody] public Example Response;

The attribute [HttpResponseJsonBody] evaluates the returned JSON text and maps it to an instance of the Example class.

It is time to run our operation again. Attach the debugger and place a breakpoint within the ‘on_complete’ block.

Below is the response which we can now use within our app.

In the next post we will take a look on how to send and receive data with assets such as BestHTTP, UniWeb, UnityHTTP, etc.

Last updated