- Home /
Parse different types of objects from Json
I'm trying to save player's inventory as a Json data, but I'm having a problem. I have a class Sword which extends abstract class Weapon which extends another abstract class Item. This is what Json file looks like with Sword and Armor object in it: `
[
{
"category": {
"type": "Weapon",
"subtype": "Sword"
},
"id": 0,
"value": 1,
"name": "Wooden Sword",
"description": "Sword made out of wood",
"slug": "wooden_sword",
"stats": {
"wear": 100,
"damage": 5
}
},
{
"category": {
"type": "Armor",
"subtype": "Helmet"
},
"id": 1337,
"value": 1,
"name": "Steel Helmet",
"description": "Whatever...",
"slug": "steel_helmet",
"stats": {
"wear": 100,
"defence": 5,
"vitality": 3,
"dexterity": -1
}
}
]
How can I possibly parse these objects into their respective C# objects?
Answer by RF008 · Aug 11, 2017 at 07:37 AM
you can use JsonUtility.FromJson your script type it will automatically convert the json to class variables
i havent tried inherited class if this did not work create and helper function to save and read the values
Yes, but how do I know which class I'm converting to? JsonUtility.FromJson needs a class name to convert the data to it.
Your answer
Follow this Question
Related Questions
I need to find inventory and item system, and to save them 0 Answers
How to make an inventory for my text advenutre? 0 Answers
Set Playerpref("String" + Int)?? 1 Answer
Saving a class List from another script 0 Answers
OpenFilePanel alternative 1 Answer