Search Unity

How to use SimpleJSON

Discussion in 'Scripting' started by CrazyNinja2000, Feb 9, 2016.

  1. CrazyNinja2000

    CrazyNinja2000

    Joined:
    Jun 21, 2015
    Posts:
    1
    I am very new to Serializers, and know nothing about them. I was only told I needed one. Right now I am trying to use SimpleJSON, but I can't find a good explanation on it. The wiki page is very non-descriptive, and as a beginner to serializers, I didn't understand it's small explanation.

    The only part I understand is that it takes variables and arrays, saves it to a JSON string, and then the JSON string is able to be saved into a file, and then you can deserialize the file into a JSON object(I think I got that right).

    What I need to know is how. Lets say I have these 2 strings and an array in an object:
    Code (CSharp):
    1. string x = "2";
    2. string y = "4";
    3. string[] z = new string[2];
    4. z [0] = "6";
    5. z [1] = "7";
    How could I save this into JSON string, and then how would I deserialize the JSON string back into the 2 strings and an array?

    Note: If there is a better or simpler serializer to use, please explain that instead. I am just looking for a simple serializer that can serialize variables to a file so it is easy to read outside the program, and then be able to turn it back into variables/objects. I don't care if it serializes as binary or text.
     
    Last edited: Feb 9, 2016
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    File.WriteAllBytes() and File.ReadAllBytes() do pretty much exactly what you're asking for; they are used for storing/reading bytes to/from files. You'll have to take care of translating them to/from variables and bytes though, hopefully this isn't a dealbreaker.