Reputation: 1109
I have a class named item in which I have more fields of ints and floats for attributes like damage, level requirement and other stuff. I wanted to keep some predefined items in a json file and when I need a new item with that set of attributes to take it out with the name from json file. Let's say I want a sword and a bow:
//json example file -- this isn't a proper file
//this is a preview of what I would like
{
godly_sword {damage: 45, level: 10},
normal_bow {damage: 4, level: 3}
}
I want to be able to take these from a json by their name and create a new object with those attributes like this:
//Creating a new item which has a preset of attributes assigned to the name godly_sword
Item weapon = json.fromJson(Item.class, "godly_sword");
I tried playing with this in different ways but all I could do is load only one preset:
//Proper json file
{
class: game.Item,
damage: 45,
level: 10
}
This works but it isn't much of a help, because I don't want to create a json file for every item I predefined. Any advice would be very helpful.
Upvotes: 1
Views: 677
Reputation: 5421
Keys should be in quotes.
{
"godly_sword" {"damage": 45, "level": 10},
"normal_bow" {"damage": 4, "level": 3}
}
You could also consider defining the weapons in an array, which you do using square brackets [].
Take a look at this tutorial on Parsing JSON in libGdx. If you're new to JSON, it explains things in much clearer terms than the libGdx wiki.
Upvotes: 1