This tutorial is amazing! I had been looking for hours for information on how to read or parse and deserialize a Json array in my C# script in unity and this awesome video is the first place I saw to mention that two different variables need to have the same name for it to work. Thank you so very much!
This was great thanks! I'm trying to figure out how to load in models/equipment based on attributes in a json file stored online. Obviously there is no tutorial for this but this has been the most helpful so far!
You just need to use unity webrequest to download the page and then use the tutorial as is docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Get.html
@@DestinedToLearn Thanks for the reply. The format is different I'll have to check into this newton json. You can probably tell I'm brand new. I appreciate your time.
@@quattrocity9620 The process is pretty similar, the built in one isn't very forgiving with format. Good luck I hope you get there with your project. I will do a sample webrequest one this afternoon as I get asked that a bit.
I get unexpected node type. I just want to get the lng latitude out of a json string. The json string looks like this in the console (ofcourse i changed all the values) "ip":"stringwithmyip","location":{"country":"countrycode","region":"provincename","city":"cityname","lat":float,"lng":float,"postalCode":"postalcode","timezone":"+02:00","geonameId":geonameid}
@@Kaasbaas1234 did you also change the variable names to match? Everything has to be perfect. I do wonder if the : in 02:00 is an issue. I can't tell what is wrong from there. If you post your script and json file to pastebin I am happy to have a lot.
@@Kaasbaas1234 If that is the format it won't work with the built in JSON reader. You could try to use the newton one from the store (I have no experience) but you easily just read in the string and make your own parser since you only want some of the data. Just use split like in my Reading text files tutorial.
@@DestinedToLearn Thanks for the help i would have been stuck on that for a while... Decided to go a different route and seperate my json into string elements, then used regular expressions to get the longitude and latitude out of the corresponding elements. this is the pastebin link for those interested qumEhgRm
If anyone is having problems with player list being populated with null values make sure your data class (in this case Player) is not inheriting from MonoBehaviour. I had the same problem and removing MonoBehaviour fixed it 🤗
video is too fast i rewatched many times and still doesn't understand anything it's too hard i'm having learning disability(ADHD makes it hard to focus things) can anyone explain step by step with simpler json file? i only need load
Hello, I tried your way to load my "hero list" but even if it finds the good "hero" count from the file, the hero class objects in the list (equivalent to your "player" class objects in your myplayerlist) are null... Can't figure out why ! Any thought ?
I would assume it is most likely naming and/or formatting of the json file. Remember it is case sensitive and that naming is important. This is the text from the sample json in the tutorial {"player":[{"name":"Warrior","health":"100","mana":20},{"name":"Wizard","health":"40","mana":80},{"name":"Healer","health":"100","mana":120}]}
Hello! Nice video. I have a question, if my json looks something like this: { "name": "Some name", "Something": "xyz", "skills" : { "run" : 3, "jump" : 4, "swim" : 6 } How should i make the class? Should i make "skills" be another class or maybe something like a struct? How do i solve this?
So i am trying to build a trivia game. My Json file contains 10 questions. I want to reach 1 specific question and its answers but couldn't manage to use them in another class.
@@xalalakos you mean extra layer of detail in the JSON? Not with the built in importer. You would need to use the free newtonsoft asset from the asset store for more complex JSON.
I know this is an ancient video, but I'm struggling in figuring out how to attach a sprite to these "player" variables. If I have stored sprites (in the resources folder) that I would like to attach to them, how would I do that?
you would have to put it in a for loop to go thru the array to print all the values. something like myplayerlist.player[0].name to access the name in the first element.
it is not loading the info under MyPlayerList... there is no error messages tho. I followed exactly what was coded. Any idea of what could potential causes this? Thanks !!!
It is hard to say without seeing the code, join my discord and share the code and I can have a look and see if I can notice your issue. linktr.ee/DestinedLinks
Great tutorial, really helps build intuition! I however have a question, what would we do if we had let's say a JSON object containing other JSON objects?
do you mean a JSON object with more levels? If so use Newtonsoft JSON from asset store (free). If you literally mean JSON in JSON I haven't heard of that so wouldn't have a clue.
@@DestinedToLearn I believe the latter. Let's say that you have a library, where there are multiple book categories, each category containing multiple books. The way you'd portray that in JSON (and correct me if I'm wrong) is that you'd have a json object (the libraru), containing multiple JSON objects (the categories) each of which contains other json objects (the books)
@@ethnicsovereignty2369 I am not that much of an expert at JSON. I just did enough for my project and then made the video so I didn't forget and that is it. Sorry I don't really know how to help :(
@@DestinedToLearn Ah, that's okay, thank you for your time! When I (inevitably) find the answer I'll be sure to post it here in case anybody else needs it
Generally I would do it one of two ways. My normal method is apply it to objects as reading in the data. Then you can do it from that script you read it in from. The second is just create public functions in the reader it script that you can call from another script to get the data. For example public int GetHealth(string type) then in that function search the array for entry of that type and return the health. The individual elements are called by myplayerlist.player[index].health where index is the element in the array you are looking for.
@@MrFarkasOfficial Good luck. In a few weeks I will have a bigger JSON example because it is my most watched video. It will be a little level editor with save/load functionality.
@@DestinedToLearn I don't understand most of what you are saying in this reply. "apply it to objects as reading in the data." I don't understand how to do this. I'm such a newbie. I really wish I understood this better, but static and non-static and serializable are all very confusing to me still.
I have different gameobjects with unique id's. I have formatted my json accordingly as you did with name. How can I get rest of the datablock according to id? ;-;
I have a problem inside my main array called "cards" lies another array "effects" consisting of strings and ints, I cannot for the life of me figure out how to access that sub array, do you have any idea how to do it? Whenever I use an object array, it fails to read in all the data from that array
you need to use newtonsoft json (free from asset store), the built in JSON reader is very restrictive of the format it works with. The newtonsoft one uses pretty much identical functions and barely adds any overhead to your project (less than 100Kb)
@@DestinedToLearn im not quite sure how to explain it, so I send a tutorial that I followed for a pair of scripts ruclips.net/video/_nRzoTzeyxU/видео.html Im trying to use the JSON file to fill the viarable "Sentences", which is a Queue class
@@maucazalv903 I don't have any experience with that but have a look at stackoverflow.com/questions/310167/in-c-how-can-i-serialize-queue-net-2-0 the last comment should work for you. To find more help on it google "serialize Queue unity"
Thanks for great help. It worked. But I have a problem if I call this script from other script in the same object with the GetComponent method, all the data is in the myPlayerList going null. Any ideas? Thanks again.
That is strange indeed, I don't see what the difference would be. Have you tried putting a debug.log in the original script on the function you are calling to make sure it is being called. Happy to have a quick look if you use pastebin to share scripts.
Hello, I have a question, I do the same thing like what you did. I desereliaze string json from web socket, but I have a problem. json utility always give me an error. I success parse the json when the json like this: string s = "{\"classes\":\"arrayResult\",\"confidences\":\"confidences\",\"data\":\"testing\"}"; => i get this json string by copying rest api result in console. but when I try to parse json from rest api, it will give me error. could you give me some advice/help? thank you so much
how are you getting the file from the web? Have you debugged it to make sure it isn't changing the string? I would use unitywebrequest to download it (there is a tutorial on my channel for how to do that).
@@DestinedToLearn thank you for your answer, my problem is solved. There is an error from web socket server, I did double string casting for data dictionary (1. From json.dumps, 2 from websocket.send_json)
Hey mate, great video, it got me started. I'm currently trying to read in this sample of json { "actionName": "staff", "values": { "damageAmount": 30, "maxActionDistance": 7 } } but unity automatically flattens it... also the properties under values are all a custom type in my game which is also leading to problems, do you have any ideas? thanks
Actually for you mean if you make changes they aren't saved? If you want to save have a look at my writing JSON tutorial which shows you how to go the other way.
@@DestinedToLearn well after 1 hour of debugging, I found out it was reading the JSON, but the JSON file is capitalized. And it's not in my script. But it works now and I got the text file running on a server right now. So I can edit the fields wherever I am
Hi! Thanks a lot It is realy nise and smooth Can you help please with the 'next step', when I want to transfer data from one script to another? Case: I have two JSON sources, both of them store some information First one keep current data And second one general leveling data When i parse both data i use Class for each of JSON files into two different scripts. Later I need to put level info (like stats for level 1 or 2 or later on), to current data and i see the error that I can not do it, like: MySpawnerData.CurrentLevelingData = LevelingReader.myLevelData.Leveling[0].LevelingData; Even when LevelingData and CurrentLevelData are tottaly same What could be a solution? Thanks a lotin advance!
Solution Found: (I used struct instead of class) Thanks! @@DestinedToLearn CS0029 Unknown implicitly converted type "LevelingReader.LevelingData" into "SpawnerReader.LevelingData". (text is translated from other language, so some words could be different)
There are 2 same classes for parsing purposes in 2 different scripts. [System.Serializable] public class LevelingData { public float Speed; public float Period; public float Height; public float Pair1; public float Pair2; public float Pair3;
HOW THE FRICK DID I ALREADY KNOW HE WAS GONNA SAY TO MAKE A SCRIPT CALLED JSONREADER I PAUSED IT AT THE START AND RANDOMLY MADE A FILE CALLED JSONREADER
@@youtuberecommended9183 No worries glad you fixed your problem. I guessing you had a naming issue. Unity JSON is very unforgiving if everything isn't exactly the way it should be.
it sounds louder than it really it is! But I actually find the typing satisfying. I can only imagine how loud it would be if I had a mechanical keyboard
This is Amazing!
Exactly what I was looking for in my project. Thank you so much!
You're very welcome! :) thanks for watching.
Short, sharp and concise. Please don't think I'm blowing smoke when I say this is the clearest RUclips tutorial I've ever come across. 10/10
thank you for the kind comments :) it gives me confidence to make more videos.
Thank you for a straightforward and simple explanation on how to use JSON. Now that I can grasp it, I can move on to more complexed JSON usages
glad it helped :)
Short and to the point: exactly the way it should be! Thank You!
thank you for feedback
This tutorial is amazing! I had been looking for hours for information on how to read or parse and deserialize a Json array in my C# script in unity and this awesome video is the first place I saw to mention that two different variables need to have the same name for it to work. Thank you so very much!
glad it helped :)
Thank you for this. It worked as described. I ended moving the two classes into their own scripts and it worked the same.
:) glad it worked. I just kept them separate cause I like keeping scripts small
Amazing! Quick and easy to follow and shows a more complex use than some tutorials!
glad you found it useful
Thank you! I was having trouble getting this to work. Your video helped me a lot!
glad it helped :D
Many thanks very much! There is no clearer and more understandable tutorial I have seen so far. :)
glad you liked it :)
This was great thanks!
I'm trying to figure out how to load in models/equipment based on attributes in a
json file stored online. Obviously there is no tutorial for this but this has been the
most helpful so far!
You just need to use unity webrequest to download the page and then use the tutorial as is docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Get.html
also if your json doesn't follow this format you will need to use the newton json for unity.
@@DestinedToLearn Thanks for the reply. The format is different I'll have to check into this newton json. You can probably tell I'm brand new. I appreciate your time.
@@quattrocity9620 The process is pretty similar, the built in one isn't very forgiving with format. Good luck I hope you get there with your project. I will do a sample webrequest one this afternoon as I get asked that a bit.
@@DestinedToLearn awesome! Subbed with notification. Look forward to it.
Thank you!
Your explanation was very concise and very helpful.
Glad you found it useful :)
Thank you! I was looking for this solution!
glad it helped
Great video! Really well described and executed.
thanks for nice comment :)
Amazing short answer! Thnx! Thumb up!
glad it helped
clear and easy, straight to the point. Thanks a lot !
glad you liked it :)
I get unexpected node type. I just want to get the lng latitude out of a json string. The json string looks like this in the console (ofcourse i changed all the values)
"ip":"stringwithmyip","location":{"country":"countrycode","region":"provincename","city":"cityname","lat":float,"lng":float,"postalCode":"postalcode","timezone":"+02:00","geonameId":geonameid}
I'm trying to only parse the lat and lng values
@@Kaasbaas1234 did you also change the variable names to match? Everything has to be perfect. I do wonder if the : in 02:00 is an issue.
I can't tell what is wrong from there. If you post your script and json file to pastebin I am happy to have a lot.
@@Kaasbaas1234 If that is the format it won't work with the built in JSON reader. You could try to use the newton one from the store (I have no experience) but you easily just read in the string and make your own parser since you only want some of the data. Just use split like in my Reading text files tutorial.
@@DestinedToLearn Thanks for the help i would have been stuck on that for a while... Decided to go a different route and seperate my json into string elements, then used regular expressions to get the longitude and latitude out of the corresponding elements. this is the pastebin link for those interested qumEhgRm
@@Kaasbaas1234 Glad you got it sorted :)
Made it really simple, Thanks!
glad you thought so :)
Exactly what I was looking for, Thanks!
glad it helped!
If anyone is having problems with player list being populated with null values make sure your data class (in this case Player) is not inheriting from MonoBehaviour. I had the same problem and removing MonoBehaviour fixed it
🤗
Glad you were able to fix that. Would have been a little tricky to find!
Thank you so much for this
glad it helped
video is too fast i rewatched many times and still doesn't understand anything it's too hard
i'm having learning disability(ADHD makes it hard to focus things)
can anyone explain step by step with simpler json file?
i only need load
sorry :(
Excellent tutorial. Thank you.
thank you for the nice feedback :)
ty, it was very well explained ans useful ! i wish u the best !
glad you found it helpful :)
Very clear, cheers.
thanks for the nice comment :)
Realliy nice!
:)
You know how to teach. 🙏
Can you do a tutorial on rewriting the values of specific keys ingame?
I have a writing to JSON tutorial which shows how to go the other way
thanks brother its really helped so much
🙂
thank you I appreciate the feedback :)
Thanks awesome tutorial!
thanks
why not use a json file and put json text in a text file?
you can use the .json extension, it works fine. I recorded the tutorial in one take and probably should have done that in hindsight.
Hello,
I tried your way to load my "hero list" but even if it finds the good "hero" count from the file, the hero class objects in the list (equivalent to your "player" class objects in your myplayerlist) are null... Can't figure out why ! Any thought ?
I would assume it is most likely naming and/or formatting of the json file. Remember it is case sensitive and that naming is important. This is the text from the sample json in the tutorial {"player":[{"name":"Warrior","health":"100","mana":20},{"name":"Wizard","health":"40","mana":80},{"name":"Healer","health":"100","mana":120}]}
Hello! Nice video. I have a question, if my json looks something like this:
{
"name": "Some name",
"Something": "xyz",
"skills" : {
"run" : 3,
"jump" : 4,
"swim" : 6
}
How should i make the class? Should i make "skills" be another class or maybe something like a struct? How do i solve this?
you might want to look at newtonsoft package in the asset store (free). The builtin on supports a very limited set of formats.
So i am trying to build a trivia game. My Json file contains 10 questions. I want to reach 1 specific question and its answers but couldn't manage to use them in another class.
I have a key-value json file, how I can get and set a value to a text object and use key as identifier?
Thank you! Very helpful!
glad it helped :)
@@DestinedToLearn I have a question though. Can the Name or the Health have a list of names and so on, in the Unity inspector?
@@xalalakos you mean extra layer of detail in the JSON? Not with the built in importer. You would need to use the free newtonsoft asset from the asset store for more complex JSON.
I know this is an ancient video, but I'm struggling in figuring out how to attach a sprite to these "player" variables. If I have stored sprites (in the resources folder) that I would like to attach to them, how would I do that?
I would import all the sprites into an array and then store an index to to sprite in that array.
Thanks man!
you are welcome :)
I hope you are still active when I try Debug.Log(myPlayerList); it prints me out JSONreader+PlayerList.... How can I print out individual json values?
you would have to put it in a for loop to go thru the array to print all the values. something like myplayerlist.player[0].name to access the name in the first element.
Thank you
glad it helped
Thank you!
Straight to the point!
thanks for commenting. I hope it helped!
thanks, it solved my query
glad it helped
I would like to know how you display all that information on the scene view.Thanks
for is you had ui text it would be something like mytext.text = player[0].name to display the name of the first player
Superb! thanks for this video!
Glad you found it useful :)
it is not loading the info under MyPlayerList... there is no error messages tho. I followed exactly what was coded. Any idea of what could potential causes this? Thanks !!!
It is hard to say without seeing the code, join my discord and share the code and I can have a look and see if I can notice your issue. linktr.ee/DestinedLinks
Great tutorial, really helps build intuition!
I however have a question, what would we do if we had let's say a JSON object containing other JSON objects?
do you mean a JSON object with more levels? If so use Newtonsoft JSON from asset store (free). If you literally mean JSON in JSON I haven't heard of that so wouldn't have a clue.
@@DestinedToLearn I believe the latter. Let's say that you have a library, where there are multiple book categories, each category containing multiple books. The way you'd portray that in JSON (and correct me if I'm wrong) is that you'd have a json object (the libraru), containing multiple JSON objects (the categories) each of which contains other json objects (the books)
apologies, I've refactored my code a bit and now I've created a multidimensional array (array of arrays of arrays)
@@ethnicsovereignty2369 I am not that much of an expert at JSON. I just did enough for my project and then made the video so I didn't forget and that is it. Sorry I don't really know how to help :(
@@DestinedToLearn Ah, that's okay, thank you for your time! When I (inevitably) find the answer I'll be sure to post it here in case anybody else needs it
Nice video!
thank you :)
Awesome! Noob question: how would one access the variables in the Player class from in game? How do I access those variables from another script?
Generally I would do it one of two ways. My normal method is apply it to objects as reading in the data. Then you can do it from that script you read it in from. The second is just create public functions in the reader it script that you can call from another script to get the data. For example public int GetHealth(string type) then in that function search the array for entry of that type and return the health. The individual elements are called by myplayerlist.player[index].health where index is the element in the array you are looking for.
@@DestinedToLearn excellent. Thank you very, very much!
@@MrFarkasOfficial Good luck. In a few weeks I will have a bigger JSON example because it is my most watched video. It will be a little level editor with save/load functionality.
@@DestinedToLearn then you've got my sub. Keep up the good work.
@@DestinedToLearn I don't understand most of what you are saying in this reply. "apply it to objects as reading in the data." I don't understand how to do this. I'm such a newbie. I really wish I understood this better, but static and non-static and serializable are all very confusing to me still.
thank you.it was very helpful!
you are welcome, glad it helped!
I have different gameobjects with unique id's. I have formatted my json accordingly as you did with name. How can I get rest of the datablock according to id? ;-;
do you want to share your json via pastebin so I can see what you are talking about?
I have a problem inside my main array called "cards" lies another array "effects" consisting of strings and ints, I cannot for the life of me figure out how to access that sub array, do you have any idea how to do it? Whenever I use an object array, it fails to read in all the data from that array
you need to use newtonsoft json (free from asset store), the built in JSON reader is very restrictive of the format it works with. The newtonsoft one uses pretty much identical functions and barely adds any overhead to your project (less than 100Kb)
What if I don't have the "player" layer in the json? Mine goes directly to "name" and "score" of each data.
Won't work with the builtin. It is very specific with the layout. The Newtonsoft one in the app stop is more forgiving of the format of the json.
How can I use this for "Queue" variables? like.. making sure that each each element of a same list got its own string
I am not quite sure what you mean
@@DestinedToLearn im not quite sure how to explain it, so I send a tutorial that I followed for a pair of scripts
ruclips.net/video/_nRzoTzeyxU/видео.html
Im trying to use the JSON file to fill the viarable "Sentences", which is a Queue class
@@maucazalv903 I don't have any experience with that but have a look at stackoverflow.com/questions/310167/in-c-how-can-i-serialize-queue-net-2-0 the last comment should work for you. To find more help on it google "serialize Queue unity"
@@DestinedToLearn thanks
@@maucazalv903 good luck!
Thanks for great help. It worked. But I have a problem if I call this script from other script in the same object with the GetComponent method, all the data is in the myPlayerList going null. Any ideas? Thanks again.
That is strange indeed, I don't see what the difference would be. Have you tried putting a debug.log in the original script on the function you are calling to make sure it is being called. Happy to have a quick look if you use pastebin to share scripts.
This was great! Thanks!
thanks for letting me know, i appreciate it. I am glad it helped!
Thank you!
Thank you for letting me know you liked it :)
Hello, I have a question, I do the same thing like what you did. I desereliaze string json from web socket, but I have a problem. json utility always give me an error.
I success parse the json when the json like this:
string s = "{\"classes\":\"arrayResult\",\"confidences\":\"confidences\",\"data\":\"testing\"}"; => i get this json string by copying rest api result in console.
but when I try to parse json from rest api, it will give me error. could you give me some advice/help? thank you so much
how are you getting the file from the web? Have you debugged it to make sure it isn't changing the string? I would use unitywebrequest to download it (there is a tutorial on my channel for how to do that).
@@DestinedToLearn thank you for your answer, my problem is solved. There is an error from web socket server, I did double string casting for data dictionary (1. From json.dumps, 2 from websocket.send_json)
@@aguspray glad it was a "simple" fix.
Do I have to system serialize it?
I don't think so, but you do it you want to see it in the editor.
Hey mate, great video, it got me started.
I'm currently trying to read in this sample of json
{
"actionName": "staff",
"values":
{
"damageAmount": 30,
"maxActionDistance": 7
}
}
but unity automatically flattens it... also the properties under values are all a custom type in my game which is also leading to problems, do you have any ideas? thanks
Use newtonsoft JSON from appstore, it is free and the syntax is basically the same but handles a much wider range of JSON
from asset store
how to return the edited values from unity to json text?
Check my writing to JSON tutorial
The playerlist doesn't get updated on launch?
think you might have a mistake then because it does for me
Actually for you mean if you make changes they aren't saved? If you want to save have a look at my writing JSON tutorial which shows you how to go the other way.
@@DestinedToLearn well after 1 hour of debugging, I found out it was reading the JSON, but the JSON file is capitalized. And it's not in my script. But it works now and I got the text file running on a server right now. So I can edit the fields wherever I am
@@yakanashe don't feel bad, we all have moments like in coding. I have had much worse myself.
@@DestinedToLearn haha thanks :)
Great video!!!!
thanks i am glad you liked it
Myplayerlist is not showing in the UNITY. Please help me its urgent.
did you put [system.Serializable] above it?
@@DestinedToLearn yes it solved the problem thank you.
@@rajatbhalla1455 No worries :)
Thank u, bro!
you are most welcome :). Thanks for watching.
Thank you 🥺
you are welcome :)
How do u do this if u have the link of json file in web
use the unitywebrequest class docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Get.html
@@DestinedToLearn Thank you...it worked
@@sauravs1425 Glad you got it figured :)
You saved me
glad it helped
Hi! Thanks a lot
It is realy nise and smooth
Can you help please with the 'next step', when I want to transfer data from one script to another?
Case:
I have two JSON sources, both of them store some information
First one keep current data
And second one general leveling data
When i parse both data i use Class for each of JSON files into two different scripts.
Later I need to put level info (like stats for level 1 or 2 or later on), to current data and i see the error that I can not do it, like:
MySpawnerData.CurrentLevelingData = LevelingReader.myLevelData.Leveling[0].LevelingData;
Even when LevelingData and CurrentLevelData are tottaly same
What could be a solution?
Thanks a lotin advance!
what is the error?
Solution Found: (I used struct instead of class)
Thanks!
@@DestinedToLearn CS0029 Unknown implicitly converted type "LevelingReader.LevelingData" into "SpawnerReader.LevelingData". (text is translated from other language, so some words could be different)
There are 2 same classes for parsing purposes in 2 different scripts.
[System.Serializable]
public class LevelingData
{
public float Speed;
public float Period;
public float Height;
public float Pair1;
public float Pair2;
public float Pair3;
}
HOW THE FRICK DID I ALREADY KNOW HE WAS GONNA SAY TO MAKE A SCRIPT CALLED JSONREADER I PAUSED IT AT THE START AND RANDOMLY MADE A FILE CALLED JSONREADER
because you can read minds
goat
lol thanks
Good tutorial
:)
@@DestinedToLearn Nevermind. It works fine my mistake.
@@youtuberecommended9183 No worries glad you fixed your problem. I guessing you had a naming issue. Unity JSON is very unforgiving if everything isn't exactly the way it should be.
very cool but...Your...Poor...Keyboard
it sounds louder than it really it is! But I actually find the typing satisfying. I can only imagine how loud it would be if I had a mechanical keyboard