Receiving http web response in JSON format, how would YOU do this?

I'm making an app that sends a request to my game website and pulls some data in JSON format. The data will be the player's account information, which would be an array with maybe 10-12 string key/value pairings. I want to make a response checker to validate that the JSON retrieved contains the needed data or if the request failed for some reason.


What I'm thinking is the JSON should be response of two arrays from the website.


Array 1 would have keys:

-"Status": 1 or 0 from the server for depending if the request was correct.

-"ErrorMessage": error message describing if any error occurred.

Array 2 would simply be an array of the players profile data:

"Player name" = player1 "Player gold" = 5000, etc Is this the right way to do it?

My options for received responses are:

-token was invalid

-token didn't exist

-token expired

-the request was correct but the access was denied (ie: tried to access an account off the wrong token, tried to attack a player that wasn't attack able, etc)

-the request correct returning all of the needed data.

If the response is one of the first 3 options should I also have a key in the first array called "tokenError"? Check if that is nul before looking for anymore data writhin the response?

Replies

There isn’t one “right way” to do this stuff. Rather, this is part of the overall design process for your app. The way I’d approach this is:

  1. Start by working ‘down’ from the top of the app. What information does the higher-level code within the app need?

  2. Build a model object that holds that data, completely independent of your networking code.

  3. Test the design by creating dummy instances of this model object (using fixed data) to see if the high-level code can work with them nicely.

  4. Once that’s out of the way, define a JSON structure that supports this model.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"