Search Unity

Web Based JSON File - How to best store it in Unity?

Discussion in 'Scripting' started by TechHead, Jul 25, 2018.

  1. TechHead

    TechHead

    Joined:
    Jan 23, 2017
    Posts:
    7
    Hi all,

    I have just discovered Unity (love it!) and am trying to upskill myself in C# as quickly as possible.

    I have a project where I want to download into memory, a web-based JSON file which has approximately 90 individual entries, with each entry having 5 associated data fields.

    I want to query this JSON data, for a particular key field each time, when running my Unity application, which in turn will be used to present the results of that particular entry onto the screen.

    I've been reading up trying to figure out how best to store this imported JSON data into memory during run-time though despite a lot of research online am still unsure as to what approach to take, eg: import it into an array, save the JSON file using a text string or similar? Or can I just leave it in a JSON object and query it each time from there?

    Any advice or suggestions much appreciated. Also, are there any must-have JSON assets? I've had a look on the assets store, though to honest, found them all a bit confusing for someone still learning such as myself.

    Thanks in advance,


    Simon
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Leaving it as text or rehydrating it into objects really has to do with your use case, how heavy the objects are, how often you access them, is it purely read-only, etc.

    Pick one way, if it proves to be a problem, do it the other way.

    Get the Newtonsoft JSON asset and call it a day:

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    There's plenty of others but they are almost guaranteed to be irritating or incomplete in some fashion. :)

    And welcome to Unity3D... hop right in, the water's fine.
     
  3. TechHead

    TechHead

    Joined:
    Jan 23, 2017
    Posts:
    7
    Hi Kurt,

    Thanks very much for the reply and advice, much appreciated.

    I'll only be referencing the data in JSON file (ie: no saving required), though I could potentially be doing quite a few queries against the data so ideally need the most performant way of doing this. Is there a particular way (eg: text, object, etc) that is faster/better than the other?

    I'll download Newtonsoft JSON and will give it a try.

    Thanks again,


    Simon

     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I use json a bit in several apps. Downloading it and then deserializing it into the proper class is the way I usually use them.

    json2csharp.com can convert your json string into the proper properties to use with json.net (what you are getting on the asset store). Or, visual studios has a way to paste the json string into a text file and create the proper classes for you.
     
  5. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I've used it too before and I just use the built in JsonUtility. I must say I generally stick to XML instead of JSON, for which (de)serialization is included in C#.
     
  6. TechHead

    TechHead

    Joined:
    Jan 23, 2017
    Posts:
    7
    Thank you both for your replies, it definitely is a learning curve - though fun! :)