Created
May 15, 2018 08:41
-
-
Save rstecca/98b07ad3fcb06b17a39cf3d987333e16 to your computer and use it in GitHub Desktop.
Unity Tips: Json Utility minimal example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
JSON minimal example. | |
To understand how object members map to json and vicecersa. | |
Experiment by changing types and values. | |
twitter: @riccardostecca | |
*/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
/// <summary> | |
/// Our example class. | |
/// Remember: JsonUtility will translate public members only | |
/// </summary> | |
class myobj | |
{ | |
public double lon; | |
public double lat; | |
public string currency; | |
public myobj(double _lon, double _lat, string _curr) | |
{ | |
lon = _lon; | |
lat = _lat; | |
currency = _curr; | |
} | |
} | |
public class JsonTest : MonoBehaviour { | |
void Start () { | |
// Build and print out the string that results from pulling a new myobj into a json string. | |
string json = JsonUtility.ToJson(new myobj(0.011122222d, 600060654.3030d, "LLL")); | |
Debug.LogFormat("{0}", json); | |
// Rebuild the object back from the json string and print its values. | |
myobj oo = JsonUtility.FromJson<myobj>(json); | |
Debug.LogFormat("Lon:{0} Lat:{1} Curr:{2}", oo.lon, oo.lat, oo.currency); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment