What is JSON?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome I'm your code monkey and in this video we're going to learn what is Jason how it's formatted and how we can use it in unity to easily save and load data let's begin so Jason is a lightweight that a format that is easy for humans to read and write but also for machines to parse and generate Jason stands for JavaScript object notation but it doesn't necessarily have anything to do with JavaScript it is a widely used data format with specific rules for creating and reading the data in unity Jason can be very useful for quickly setting up a safe system unity by default already has some functions to help you generate JSON from a class and read it back so let's quickly take a look at the adjacent format jason is a text format with some simple rules you have an object represented with opening and closing curly brackets inside the object you can have multiple fields each field is composed of a name and its value the name is a string then you add a colon to separate the name from the value the value can be represented as various things it can be a string number object array true/false or no and we can store multiple fields by using a comma to separate each field so in here I have notepad open and let's manually create a JSON object so first of all you start open and closed curly brackets then we type a string for our field name and in here let's say it's tell and then we give it a value so in here for the value who want the numbers and let's say 80 to add a second field we add a comma and then let's say help max with a value of 100 there you go this is a very simple JSON file I have saved it as test not Jason and most browsers actually have an easy way to view JSON data so in here I have opened the file in Firefox and you can see it displays the fields and eval if you click in here you can view the raw data which is example like that okay that's how the JSON format works now let's go to unity and figure out how we can use this so in here let's begin by making a script that we can use to test our code so make an easy sheriff script and name it game Hamill let's drag it out to an object let's say I want to make sure this object is running okay there's a consular code is running great alright so in here let's make a simple class so here do a private class and let's name it player data now inside let's make a position and a health amount so a public vector3 for the position and a public end for the help so this class would represent player data in your game we would use it for example for saving and loading alright so now let's go up here to the start and let's create a new instance of player data so player data let's set the position to a new vector3 and let's say on 5 0 and player data tone let's say 8 ok so we have our player data object now let's grab the adjacent string for this particular object so in order to do that we go into JSON utility and use the function to jason this function takes an object which in this case will be player data and this function returns a JSON string so let's do a debug log on that string and see what comes out all right there's the output you can see a position with an axe of 5 Y of 0 Z 0 any health of 80 this is a correct JSON object so we have successfully converted the data from an object into a string that we can easily use to save and load so for example in order to make a very simple safe system you could easily save this JSON string into a file now let's look into how we can parse a JSON string back into an object so in our code here after we get the JSON string we can parse it into an object by using JSON utility dot from jason this function takes the type of the object that we wish to parse so in this case player data and we're going to send our JSON string so this will return a player data object so player down and let's say loaded player that so with that let's print out the contents to make sure that we wanted to correct alright there it is the JSON string contains position five zero zero health of eighty and then we parse the JSON string back into our object and our object does contain a position which is a vector three five zero zero and health of eighty so you can see we successfully took a player data convert it into JSON and then back again into player data so this is a very easy way to store your game State you would create a class that store the data that you want to save parse it into Jason save it into a file then unload you wouldn't load the text from that file and parse it back into Jason so let's test that really quick in here we have our JSON string let's save that into a file and since jason is a string that is very easy we just have to go up here and do using system dot io and then we can go down here and use file dot write all text let's store into application dot data path was save file dot jason and the text that we're going to store is our jason string so let's comment out don't love it for now and see if that file shows up all right there you we ran our code and you can see that you have a file in here and it does contain that jason string so now that we have our save file let's read that file and parse it back from jason so in here let's comment out being safe code let's uncomment these and in here we're going to grab our jason string through file dot read all tanks and we're going to read exactly from that path that we see so go up here and copy this alright so that's it we are reading the text that we saved previously so let's see that yep there it is position five zero zero health of eighty which is exactly what is stored on the adjacent file so we unload our player data from an external file and parse adjacent to grab the object values so there you have it we learn what is jason how its structured and how we can use the new unity to make a very simple save system as always you can download the project files in utilities from unity code monkey comm if you have any questions post them in the comments and I'll do my best answer subscribe for more and I'll see you next time
Info
Channel: Code Monkey
Views: 94,344
Rating: undefined out of 5
Keywords: unity json, unity json tutorial, unity jsonutility, code monkey, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity 3d tutorials, unity tutorial 2d, unity game, unity 2018, beginner, easy, unity2d, unity3d, unity, basics, basic, game dev, code, programming, clean code, unity clean code, how to, learn, tutorial, course, game, game development, series, tutorials, games, coding, json, json data, json format, json tutorial, json c#
Id: 4oRVMCRCvN0
Channel Id: undefined
Length: 6min 59sec (419 seconds)
Published: Mon Aug 27 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.