Hello. I’m a bit of a NOOB to API calls and JSON serialization, so I user API Buddy from the Asset store to GET the JSON file for me. Which, It came through perfectly. However, other than just printing the data, how can I actually use this? What I actually need to do is take some metadata that is being pulled through the Vuforia Cloud (Just numbers), match that up with the corresponding gameTokenID field, and display the tokenDescription field.
I’ve been looking through the UNity docs, as well as Youtube and I can’t figure out what method I should be using. My understanding is that it’s already deserialized? As I said, I’m a beginner at this as well as C#, so I’m not quite sure where to go from here.
Here is my code (URL-s and Auth tokens hidden for protection)
targetData.json
{
"tokenInfo":{
"data": [
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "This is the description for the first Clue",
"gameTokenID": 1,
"tokenClue": "Clue"
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "This is the description for the second Clue",
"gameTokenID": 2,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "This is the description for the third Clue",
"gameTokenID": 3,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 4,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 5,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 6,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 7,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 8,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 9,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 10,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 11,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 12,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 13,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 14,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 15,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 16,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 17,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 18,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 19,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 20,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 21,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 22,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 23,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 24,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 25,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 26,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 27,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 28,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 29,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 30,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 31,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 32,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 33,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 34,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 35,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 36,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 37,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 38,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 39,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 40,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 41,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 42,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 43,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 44,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 45,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 46,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 47,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 48,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 49,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 50,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 51,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 52,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 53,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 54,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 55,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 56,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 57,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 58,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 59,
"tokenClue": ""
},
{
"tokenIcon": "",
"brandName": "The Ritz-Carlton",
"tokenDescription": "",
"gameTokenID": 60,
"tokenClue": ""
}
]
,
"count": 60
}
}
Relevant Parts to MainManager
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using LitJson;
using System.Runtime.InteropServices;
using Vuforia;
using PerspecDev;
public class MainManager : MonoBehaviour
{
public int User_ID = 1;
public int MaxTokenCount = 60;
private String tokenNumber;
#if UNITY_IPHONE
[DllImport("__Internal")]
private static extern int GetUserID ();
#endif
public GameObject ResultObj;
public GameObject ButtonObj;
public Text winText;
private bool[] bTokenList;
// Use this for initialization
void Start()
{
bTokenList = new bool[MaxTokenCount];
for (int i = 0; i < MaxTokenCount; i++)
{
string stToken = "token" + (i + 1).ToString();
if (!PlayerPrefs.HasKey(stToken))
{
PlayerPrefs.SetInt(stToken, 0);
bTokenList[i] = false;
}
else
{
if (PlayerPrefs.GetInt(stToken) == 1)
{
bTokenList[i] = true;
}
else
{
bTokenList[i] = false;
}
}
}
CreateCacheDir();
winText.text = "Clue: ";
public void FindResult(string stMetadata)
{
if (stMetadata == "No")
{
//return null;
}
else
{
int iIndex = 0;
int.TryParse(stMetadata, out iIndex);
//Sets token based on metadata
bTokenList[iIndex] = true;
PlayerPrefs.SetInt("token" + iIndex.ToString(), 1);
StartCoroutine(LoadImage(stGetURL + stMetadata + ".png"));
StartCoroutine(LoadImage(txGetURL + "/?tokenID=" + stMetadata));
// Debug.Log(txGetURL + "?tokenID=" + stMetadata);
//Debug.Log("The Token Url is" + tokenUrl);
tokenNumber = stMetadata;
//winText.text = "Clue: " + "Add in dynamic clue here";
winText.text = "Clue: " + _outputText;
StartCoroutine(SendRequest(iIndex));
RequestTokenData(tokenNumber);
}
}
public class SteamResponseData : APIBuddy.WebResponseData
{
public AppNews tokenInfo; // This matches the "tokenInfo" element in the JSON response from the API.
public class AppNews : APIBuddy.WebResponseData
{
public TokenData[] data; // This matches the "data" array element in the JSON response.
}
public class TokenData : APIBuddy.WebResponseData
{
// These match the elements in each data item in the JSON response.
public string tokenIcon;
public string brandName;
public string tokenDescription;
public string gameTokenID;
public string tokenClue;
}
}
private string _outputText = "";
public void RequestTokenData(string tokenNumber)
{
_outputText = "";
// Simply pass in the URL for the API endpoint and wait for the response...
string url = "http://thisisthepathto.com/targetData.json";
APIBuddy.GetInstance().SendGetRequest<SteamResponseData>(url, 30.0f, (bool success, string errorMessage, int statusCode, object responseObject) => {
if (success)
{
// APIBuddy handles populating SteamResponseData for us, based on the fields we set up in it up above. All we need to do now is
// cast responseObject to the SteamResponseData type and then read our data!
SteamResponseData steamResponseData = (SteamResponseData)responseObject;
_outputText = "";
if (steamResponseData.tokenInfo != null && steamResponseData.tokenInfo.data != null )
{
foreach (SteamResponseData.TokenData data in steamResponseData.tokenInfo.data)
{
_outputText += "\n(" + data.gameTokenID + ")";
_outputText += "==== " + data.tokenDescription + " ====";
}
}
}
else
{
_outputText = "Error: " + errorMessage;
}
});
}
}