Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

JSON / Web Services?

Discussion in 'Scripting' started by dmennenoh, Nov 5, 2015.

  1. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    New in Unity here and an AS3 developer. Having a tough time figuring out the best/easiest way to send and receive JSON from some web services.

    In AS3 it's something like this:
    Code (csharp):
    1. var js:String = JSON.stringify({"userName":auth.user, "password":auth.pwd});
    2.  
    3. var req:URLRequest = new URLRequest(BASE_URL + "authorize/validateuser");          
    4. req.method = URLRequestMethod.POST;
    5. req.data = js;
    6. req.requestHeaders.push(new URLRequestHeader("Content-type", "application/json"));
    7. req.requestHeaders.push(new URLRequestHeader("Accept", "application/json"));
    8.          
    9. var lo:URLLoader = new URLLoader();
    10. lo.addEventListener(Event.COMPLETE, gotToken, false, 0, true);
    11. lo.addEventListener(IOErrorEvent.IO_ERROR, tokenError, false, 0, true);
    12. lo.load(req);
    It's just so nice and easy in AS3...
    Is there a relatively pain free way to accomplish this in Unity/C#?
    I've found the JSONObject utility but wondering if there's any built in ways in Unity or .NET?

    Also how to easily send/get data from a service? Do I use the WWW class?
     
  2. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    Just had this thought - since AS3 is an ECMA Script language - like JavaScript - would it be better for someone coming from AS3 to use JS in Unity, instead of C#?
     
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    You can use either. I've done both AS3 and C# and find the switchover quite painless. As for JSON, there's loads of decent free JSON API's on the Asset Store, like you saw. There's also plugins you can add into Unity, like this:

    https://github.com/mviranyi/litjson/tree/master/doc/quickstart


    I made mine in a very messy way (early draft version), but you get an idea of how it's put together.

    Code (CSharp):
    1. protected virtual IEnumerator LoadDataCache(DataObject _data)
    2.             {
    3.                 JsonReader json_reader = new JsonReader(File.ReadAllText(Application.dataPath + path + "OfflineDataCache" + extension));
    4.                 JsonData data = JsonMapper.ToObject<JsonData>(json_reader);
    5.  
    6.                 string openingText = "{   \"bulletInstance\":[";
    7.                 string originalData = JsonMapper.ToJson(data);
    8.                 string newData = JsonMapper.ToJson(_data);
    9.                 string finalText = null;
    10.  
    11.                 if (!originalData.ToString().Contains("bulletInstance"))
    12.                 {
    13.                     finalText = openingText + newData.ToString() + "]\n}";
    14.                 }
    15.                 else
    16.                 {
    17.                     string[] originalSections = originalData.Split('}');
    18.  
    19.                     for(int i=0; i < originalSections.Length - 1; i++)
    20.                     {
    21.                         if(finalText == null)
    22.                         {
    23.                             finalText = originalSections[i];
    24.                         }
    25.                         else
    26.                         {
    27.                             if(!originalSections[i].Contains("]"))
    28.                             {
    29.                                 if(originalSections[i].Contains(",{"))
    30.                                 {
    31.                                     finalText += "} " + originalSections[i];
    32.                                 }
    33.                                 else
    34.                                 {
    35.                                     finalText += "}, " + originalSections[i];
    36.                                 }
    37.                             }
    38.                         }
    39.                     }
    40.                     finalText += "}, \n" + newData.ToString() + "]\n}";
    41.                 }
    42.  
    43.                 File.WriteAllText(Application.dataPath + path + "OfflineDataCache" + extension, finalText);
    44.            
    45.                 cacheState = CacheState.Connect;
    46.                 yield return null;
    47.             }
     
    Last edited: Nov 5, 2015
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    I believe there is a .Net version of JSON, however Unity hasn't updated their compiler so we're not able to include it in Unity projects (yet). There is a slew of plugins and the Asset Store things you found.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,342
    There's no JavaScript in Unity. There's UnityScript, which is .Net with JS makeup. It's called "JavaScript" in the docs and the files are .js, but other than that they're not even close.