Search Unity

Json Manipulation with API Buddy

Discussion in 'Scripting' started by VFurukawa, Nov 12, 2017.

  1. VFurukawa

    VFurukawa

    Joined:
    Jul 4, 2017
    Posts:
    6
    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
    Code (csharp):
    1.  
    2. {
    3.    "tokenInfo":{
    4.    "data": [
    5.       {
    6.          "tokenIcon": "",
    7.          "brandName": "The Ritz-Carlton",
    8.          "tokenDescription": "This is the description for the first Clue",
    9.          "gameTokenID": 1,
    10.          "tokenClue": "Clue"
    11.       },
    12.       {
    13.          "tokenIcon": "",
    14.          "brandName": "The Ritz-Carlton",
    15.          "tokenDescription": "This is the description for the second Clue",
    16.          "gameTokenID": 2,
    17.          "tokenClue": ""
    18.       },
    19.       {
    20.          "tokenIcon": "",
    21.          "brandName": "The Ritz-Carlton",
    22.          "tokenDescription": "This is the description for the third Clue",
    23.          "gameTokenID": 3,
    24.          "tokenClue": ""
    25.       },
    26.       {
    27.          "tokenIcon": "",
    28.          "brandName": "The Ritz-Carlton",
    29.          "tokenDescription": "",
    30.          "gameTokenID": 4,
    31.          "tokenClue": ""
    32.       },
    33.       {
    34.          "tokenIcon": "",
    35.          "brandName": "The Ritz-Carlton",
    36.          "tokenDescription": "",
    37.          "gameTokenID": 5,
    38.          "tokenClue": ""
    39.       },
    40.       {
    41.          "tokenIcon": "",
    42.          "brandName": "The Ritz-Carlton",
    43.          "tokenDescription": "",
    44.          "gameTokenID": 6,
    45.          "tokenClue": ""
    46.       },
    47.       {
    48.          "tokenIcon": "",
    49.          "brandName": "The Ritz-Carlton",
    50.          "tokenDescription": "",
    51.          "gameTokenID": 7,
    52.          "tokenClue": ""
    53.       },
    54.       {
    55.          "tokenIcon": "",
    56.          "brandName": "The Ritz-Carlton",
    57.          "tokenDescription": "",
    58.          "gameTokenID": 8,
    59.          "tokenClue": ""
    60.       },
    61.       {
    62.          "tokenIcon": "",
    63.          "brandName": "The Ritz-Carlton",
    64.          "tokenDescription": "",
    65.          "gameTokenID": 9,
    66.          "tokenClue": ""
    67.       },
    68.       {
    69.          "tokenIcon": "",
    70.          "brandName": "The Ritz-Carlton",
    71.          "tokenDescription": "",
    72.          "gameTokenID": 10,
    73.          "tokenClue": ""
    74.       },
    75.       {
    76.          "tokenIcon": "",
    77.          "brandName": "The Ritz-Carlton",
    78.          "tokenDescription": "",
    79.          "gameTokenID": 11,
    80.          "tokenClue": ""
    81.       },
    82.       {
    83.          "tokenIcon": "",
    84.          "brandName": "The Ritz-Carlton",
    85.          "tokenDescription": "",
    86.          "gameTokenID": 12,
    87.          "tokenClue": ""
    88.       },
    89.       {
    90.          "tokenIcon": "",
    91.          "brandName": "The Ritz-Carlton",
    92.          "tokenDescription": "",
    93.          "gameTokenID": 13,
    94.          "tokenClue": ""
    95.       },
    96.       {
    97.          "tokenIcon": "",
    98.          "brandName": "The Ritz-Carlton",
    99.          "tokenDescription": "",
    100.          "gameTokenID": 14,
    101.          "tokenClue": ""
    102.       },
    103.       {
    104.          "tokenIcon": "",
    105.          "brandName": "The Ritz-Carlton",
    106.          "tokenDescription": "",
    107.          "gameTokenID": 15,
    108.          "tokenClue": ""
    109.       },
    110.       {
    111.          "tokenIcon": "",
    112.          "brandName": "The Ritz-Carlton",
    113.          "tokenDescription": "",
    114.          "gameTokenID": 16,
    115.          "tokenClue": ""
    116.       },
    117.       {
    118.          "tokenIcon": "",
    119.          "brandName": "The Ritz-Carlton",
    120.          "tokenDescription": "",
    121.          "gameTokenID": 17,
    122.          "tokenClue": ""
    123.       },
    124.       {
    125.          "tokenIcon": "",
    126.          "brandName": "The Ritz-Carlton",
    127.          "tokenDescription": "",
    128.          "gameTokenID": 18,
    129.          "tokenClue": ""
    130.       },
    131.       {
    132.          "tokenIcon": "",
    133.          "brandName": "The Ritz-Carlton",
    134.          "tokenDescription": "",
    135.          "gameTokenID": 19,
    136.          "tokenClue": ""
    137.       },
    138.       {
    139.          "tokenIcon": "",
    140.          "brandName": "The Ritz-Carlton",
    141.          "tokenDescription": "",
    142.          "gameTokenID": 20,
    143.          "tokenClue": ""
    144.       },
    145.       {
    146.          "tokenIcon": "",
    147.          "brandName": "The Ritz-Carlton",
    148.          "tokenDescription": "",
    149.          "gameTokenID": 21,
    150.          "tokenClue": ""
    151.       },
    152.       {
    153.          "tokenIcon": "",
    154.          "brandName": "The Ritz-Carlton",
    155.          "tokenDescription": "",
    156.          "gameTokenID": 22,
    157.          "tokenClue": ""
    158.       },
    159.       {
    160.          "tokenIcon": "",
    161.          "brandName": "The Ritz-Carlton",
    162.          "tokenDescription": "",
    163.          "gameTokenID": 23,
    164.          "tokenClue": ""
    165.       },
    166.       {
    167.          "tokenIcon": "",
    168.          "brandName": "The Ritz-Carlton",
    169.          "tokenDescription": "",
    170.          "gameTokenID": 24,
    171.          "tokenClue": ""
    172.       },
    173.       {
    174.          "tokenIcon": "",
    175.          "brandName": "The Ritz-Carlton",
    176.          "tokenDescription": "",
    177.          "gameTokenID": 25,
    178.          "tokenClue": ""
    179.       },
    180.       {
    181.          "tokenIcon": "",
    182.          "brandName": "The Ritz-Carlton",
    183.          "tokenDescription": "",
    184.          "gameTokenID": 26,
    185.          "tokenClue": ""
    186.       },
    187.       {
    188.          "tokenIcon": "",
    189.          "brandName": "The Ritz-Carlton",
    190.          "tokenDescription": "",
    191.          "gameTokenID": 27,
    192.          "tokenClue": ""
    193.       },
    194.       {
    195.          "tokenIcon": "",
    196.          "brandName": "The Ritz-Carlton",
    197.          "tokenDescription": "",
    198.          "gameTokenID": 28,
    199.          "tokenClue": ""
    200.       },
    201.       {
    202.          "tokenIcon": "",
    203.          "brandName": "The Ritz-Carlton",
    204.          "tokenDescription": "",
    205.          "gameTokenID": 29,
    206.          "tokenClue": ""
    207.       },
    208.       {
    209.          "tokenIcon": "",
    210.          "brandName": "The Ritz-Carlton",
    211.          "tokenDescription": "",
    212.          "gameTokenID": 30,
    213.          "tokenClue": ""
    214.       },
    215.       {
    216.          "tokenIcon": "",
    217.          "brandName": "The Ritz-Carlton",
    218.          "tokenDescription": "",
    219.          "gameTokenID": 31,
    220.          "tokenClue": ""
    221.       },
    222.       {
    223.          "tokenIcon": "",
    224.          "brandName": "The Ritz-Carlton",
    225.          "tokenDescription": "",
    226.          "gameTokenID": 32,
    227.          "tokenClue": ""
    228.       },
    229.       {
    230.          "tokenIcon": "",
    231.          "brandName": "The Ritz-Carlton",
    232.          "tokenDescription": "",
    233.          "gameTokenID": 33,
    234.          "tokenClue": ""
    235.       },
    236.       {
    237.          "tokenIcon": "",
    238.          "brandName": "The Ritz-Carlton",
    239.          "tokenDescription": "",
    240.          "gameTokenID": 34,
    241.          "tokenClue": ""
    242.       },
    243.       {
    244.          "tokenIcon": "",
    245.          "brandName": "The Ritz-Carlton",
    246.          "tokenDescription": "",
    247.          "gameTokenID": 35,
    248.          "tokenClue": ""
    249.       },
    250.       {
    251.          "tokenIcon": "",
    252.          "brandName": "The Ritz-Carlton",
    253.          "tokenDescription": "",
    254.          "gameTokenID": 36,
    255.          "tokenClue": ""
    256.       },
    257.       {
    258.          "tokenIcon": "",
    259.          "brandName": "The Ritz-Carlton",
    260.          "tokenDescription": "",
    261.          "gameTokenID": 37,
    262.          "tokenClue": ""
    263.       },
    264.       {
    265.          "tokenIcon": "",
    266.          "brandName": "The Ritz-Carlton",
    267.          "tokenDescription": "",
    268.          "gameTokenID": 38,
    269.          "tokenClue": ""
    270.       },
    271.       {
    272.          "tokenIcon": "",
    273.          "brandName": "The Ritz-Carlton",
    274.          "tokenDescription": "",
    275.          "gameTokenID": 39,
    276.          "tokenClue": ""
    277.       },
    278.       {
    279.          "tokenIcon": "",
    280.          "brandName": "The Ritz-Carlton",
    281.          "tokenDescription": "",
    282.          "gameTokenID": 40,
    283.          "tokenClue": ""
    284.       },
    285.       {
    286.          "tokenIcon": "",
    287.          "brandName": "The Ritz-Carlton",
    288.          "tokenDescription": "",
    289.          "gameTokenID": 41,
    290.          "tokenClue": ""
    291.       },
    292.       {
    293.          "tokenIcon": "",
    294.          "brandName": "The Ritz-Carlton",
    295.          "tokenDescription": "",
    296.          "gameTokenID": 42,
    297.          "tokenClue": ""
    298.       },
    299.       {
    300.          "tokenIcon": "",
    301.          "brandName": "The Ritz-Carlton",
    302.          "tokenDescription": "",
    303.          "gameTokenID": 43,
    304.          "tokenClue": ""
    305.       },
    306.       {
    307.          "tokenIcon": "",
    308.          "brandName": "The Ritz-Carlton",
    309.          "tokenDescription": "",
    310.          "gameTokenID": 44,
    311.          "tokenClue": ""
    312.       },
    313.       {
    314.          "tokenIcon": "",
    315.          "brandName": "The Ritz-Carlton",
    316.          "tokenDescription": "",
    317.          "gameTokenID": 45,
    318.          "tokenClue": ""
    319.       },
    320.       {
    321.          "tokenIcon": "",
    322.          "brandName": "The Ritz-Carlton",
    323.          "tokenDescription": "",
    324.          "gameTokenID": 46,
    325.          "tokenClue": ""
    326.       },
    327.       {
    328.          "tokenIcon": "",
    329.          "brandName": "The Ritz-Carlton",
    330.          "tokenDescription": "",
    331.          "gameTokenID": 47,
    332.          "tokenClue": ""
    333.       },
    334.       {
    335.          "tokenIcon": "",
    336.          "brandName": "The Ritz-Carlton",
    337.          "tokenDescription": "",
    338.          "gameTokenID": 48,
    339.          "tokenClue": ""
    340.       },
    341.       {
    342.          "tokenIcon": "",
    343.          "brandName": "The Ritz-Carlton",
    344.          "tokenDescription": "",
    345.          "gameTokenID": 49,
    346.          "tokenClue": ""
    347.       },
    348.       {
    349.          "tokenIcon": "",
    350.          "brandName": "The Ritz-Carlton",
    351.          "tokenDescription": "",
    352.          "gameTokenID": 50,
    353.          "tokenClue": ""
    354.       },
    355.       {
    356.          "tokenIcon": "",
    357.          "brandName": "The Ritz-Carlton",
    358.          "tokenDescription": "",
    359.          "gameTokenID": 51,
    360.          "tokenClue": ""
    361.       },
    362.       {
    363.          "tokenIcon": "",
    364.          "brandName": "The Ritz-Carlton",
    365.          "tokenDescription": "",
    366.          "gameTokenID": 52,
    367.          "tokenClue": ""
    368.       },
    369.       {
    370.          "tokenIcon": "",
    371.          "brandName": "The Ritz-Carlton",
    372.          "tokenDescription": "",
    373.          "gameTokenID": 53,
    374.          "tokenClue": ""
    375.       },
    376.       {
    377.          "tokenIcon": "",
    378.          "brandName": "The Ritz-Carlton",
    379.          "tokenDescription": "",
    380.          "gameTokenID": 54,
    381.          "tokenClue": ""
    382.       },
    383.       {
    384.          "tokenIcon": "",
    385.          "brandName": "The Ritz-Carlton",
    386.          "tokenDescription": "",
    387.          "gameTokenID": 55,
    388.          "tokenClue": ""
    389.       },
    390.       {
    391.          "tokenIcon": "",
    392.          "brandName": "The Ritz-Carlton",
    393.          "tokenDescription": "",
    394.          "gameTokenID": 56,
    395.          "tokenClue": ""
    396.       },
    397.       {
    398.          "tokenIcon": "",
    399.          "brandName": "The Ritz-Carlton",
    400.          "tokenDescription": "",
    401.          "gameTokenID": 57,
    402.          "tokenClue": ""
    403.       },
    404.       {
    405.          "tokenIcon": "",
    406.          "brandName": "The Ritz-Carlton",
    407.          "tokenDescription": "",
    408.          "gameTokenID": 58,
    409.          "tokenClue": ""
    410.       },
    411.       {
    412.          "tokenIcon": "",
    413.          "brandName": "The Ritz-Carlton",
    414.          "tokenDescription": "",
    415.          "gameTokenID": 59,
    416.          "tokenClue": ""
    417.       },
    418.       {
    419.          "tokenIcon": "",
    420.          "brandName": "The Ritz-Carlton",
    421.          "tokenDescription": "",
    422.          "gameTokenID": 60,
    423.          "tokenClue": ""
    424.       }
    425.    ]
    426.    ,
    427.    "count": 60
    428. }
    429. }
    430.  
    Relevant Parts to MainManager
    Code (csharp):
    1.  
    2.  
    3. using System;
    4. using System.IO;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7. using UnityEngine;
    8. using UnityEngine.UI;
    9. using UnityEngine.Networking;
    10. using LitJson;
    11. using System.Runtime.InteropServices;
    12. using Vuforia;
    13. using PerspecDev;
    14.  
    15. public class MainManager : MonoBehaviour
    16. {
    17.  
    18.     public int User_ID = 1;
    19.     public int MaxTokenCount = 60;
    20.     private String tokenNumber;
    21.  
    22. #if UNITY_IPHONE
    23.     [DllImport("__Internal")]
    24.     private static extern int GetUserID ();
    25. #endif
    26.  
    27.     public GameObject ResultObj;
    28.     public GameObject ButtonObj;
    29.     public Text winText;
    30.  
    31.     private bool[] bTokenList;
    32.  
    33.     // Use this for initialization
    34.     void Start()
    35.     {
    36.  
    37.         bTokenList = new bool[MaxTokenCount];
    38.  
    39.         for (int i = 0; i < MaxTokenCount; i++)
    40.         {
    41.  
    42.             string stToken = "token" + (i + 1).ToString();
    43.  
    44.             if (!PlayerPrefs.HasKey(stToken))
    45.             {
    46.                 PlayerPrefs.SetInt(stToken, 0);
    47.                 bTokenList[i] = false;
    48.             }
    49.             else
    50.             {
    51.                 if (PlayerPrefs.GetInt(stToken) == 1)
    52.                 {
    53.                     bTokenList[i] = true;
    54.                 }
    55.                 else
    56.                 {
    57.                     bTokenList[i] = false;
    58.                 }
    59.             }
    60.         }
    61.         CreateCacheDir();
    62.         winText.text = "Clue:  ";
    63.  
    64.     public void FindResult(string stMetadata)
    65.     {
    66.         if (stMetadata == "No")
    67.         {
    68.             //return null;
    69.         }
    70.         else
    71.         {
    72.  
    73.             int iIndex = 0;
    74.             int.TryParse(stMetadata, out iIndex);
    75.             //Sets token based on metadata
    76.             bTokenList[iIndex] = true;
    77.             PlayerPrefs.SetInt("token" + iIndex.ToString(), 1);
    78.  
    79.             StartCoroutine(LoadImage(stGetURL + stMetadata + ".png"));
    80.             StartCoroutine(LoadImage(txGetURL + "/?tokenID=" + stMetadata));
    81.             // Debug.Log(txGetURL + "?tokenID=" + stMetadata);
    82.             //Debug.Log("The Token Url is" + tokenUrl);
    83.             tokenNumber = stMetadata;
    84.             //winText.text = "Clue:  " + "Add in dynamic clue here";
    85.             winText.text = "Clue:  " + _outputText;
    86.             StartCoroutine(SendRequest(iIndex));
    87.             RequestTokenData(tokenNumber);
    88.         }
    89.     }
    90.  
    91.  
    92.  
    93.  
    94.     public class SteamResponseData : APIBuddy.WebResponseData
    95.     {
    96.  
    97.         public AppNews tokenInfo; // This matches the "tokenInfo" element in the JSON response from the API.
    98.  
    99.         public class AppNews : APIBuddy.WebResponseData
    100.         {
    101.             public TokenData[] data; // This matches the "data" array element in the JSON response.
    102.         }
    103.  
    104.         public class TokenData : APIBuddy.WebResponseData
    105.         {
    106.             // These match the elements in each data item in the JSON response.
    107.             public string tokenIcon;
    108.             public string brandName;
    109.             public string tokenDescription;
    110.             public string gameTokenID;
    111.             public string tokenClue;
    112.         }
    113.  
    114.     }
    115.  
    116.     private string _outputText = "";
    117.  
    118.  
    119.     public void RequestTokenData(string tokenNumber)
    120.     {
    121.         _outputText = "";
    122.  
    123.         // Simply pass in the URL for the API endpoint and wait for the response...
    124.         string url = "http://thisisthepathto.com/targetData.json";
    125.         APIBuddy.GetInstance().SendGetRequest<SteamResponseData>(url, 30.0f, (bool success, string errorMessage, int statusCode, object responseObject) => {
    126.             if (success)
    127.             {
    128.                 // APIBuddy handles populating SteamResponseData for us, based on the fields we set up in it up above.  All we need to do now is
    129.                 // cast responseObject to the SteamResponseData type and then read our data!
    130.                 SteamResponseData steamResponseData = (SteamResponseData)responseObject;
    131.                 _outputText = "";
    132.  
    133.                 if (steamResponseData.tokenInfo != null && steamResponseData.tokenInfo.data != null )
    134.                 {
    135.                     foreach (SteamResponseData.TokenData data in steamResponseData.tokenInfo.data)
    136.                     {
    137.                             _outputText += "\n(" + data.gameTokenID + ")";
    138.                             _outputText += "==== " + data.tokenDescription + " ====";
    139.                      
    140.                     }
    141.                 }
    142.             }
    143.             else
    144.             {
    145.                 _outputText = "Error: " + errorMessage;
    146.             }
    147.         });
    148.     }
    149.  
    150. }
    151.  
     
  2. VFurukawa

    VFurukawa

    Joined:
    Jul 4, 2017
    Posts:
    6
    Solved it myself. The answer was manipulating the data inside of the Request Token Data Function:
    Code (csharp):
    1.  
    2.  if (steamResponseData.tokenInfo != null && steamResponseData.tokenInfo.data != null)
    3.                 {
    4.                     foreach (SteamResponseData.TokenData data in steamResponseData.tokenInfo.data)
    5.                     {
    6.                        // _outputText += "\n(" + data.gameTokenID + ")";
    7.                        // _outputText += "==== " + data.tokenDescription + " ====";
    8.                         if (data.gameTokenID == tokenNumber){
    9.                             _outputText += "\n ==== " + data.tokenDescription + " ====";
    10.                         }
    11.                     }
    12.                 }
    13.