- Home /
This question was
closed Dec 29, 2016 at 01:30 AM by
$$anonymous$$ for the following reason:
The question is answered, right answer was accepted
How to get object array from json?
Hello guys! Im using litjson to get info from .json files. Everything works fine: I have a class with a constructor, that is filled like this:
private JsonData json_monsterData;
void Awake() {
json_monsterData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/" + "Monsters" + ".json"));
InitDatabase_Monster();
}
void InitDatabase_Monster() {
foreach(JsonData monster in json_monsterData as IList) {
database_Monsters.Add(new Monster((int) monster["id"],
monster["monsterName"].ToString(),
monster["spriteName"].ToString(),
monster["description"].ToString(),
(int) monster["stats"]["health"],
(int) monster["stats"]["dodge"],
(int) monster["stats"]["defence"],
(int) monster["stats"]["damage"],
(double) monster["stats"]["prepareTime"],
(double) monster["stats"]["swingTime"]
));
}
}
Here is class:
public class Monster
{
public int ID { get; set; }
public string MonsterName { get; set; }
public string SpriteName { get; set; }
public string Description { get; set; }
public int Health { get; set; }
public int Dodge { get; set; }
public int Defence { get; set; }
public int Damage { get; set; }
public double PrepareTime { get; set; }
public double SwingTime { get; set; }
public Monster(int id, string monsterName, string spriteName, string description, int health, int dodge, int defence, int damage, double prepareTime , double swingTime)
{
this.ID = id;
this.MonsterName = monsterName;
this.SpriteName = spriteName;
this.Description = description;
this.Health = health;
this.Dodge = dodge;
this.Defence = defence;
this.Damage = damage;
this.PrepareTime = prepareTime;
this.SwingTime = swingTime;
}
}
This works perfectly, but now i need to get an array of objects from a file like this:
[
{
"id":0,
"monsterName":"Ghoul",
"spriteName":"Enemy sprites",
"description":"Danger",
"stats":{
"health":1000,
"dodge":8,
"defence":6,
"damage":24,
"prepareTime":2.0,
"swingTime":15.0
},
"Reward":[
{
"item_ID":0,
"Chance_To_Get":1,
"Priority":1,
"Min_Count":"1,
"Max_Count":100
},
{
"item_ID":0,
"Chance_To_Get":1,
"Priority":1,
"Min_Count":1,
"Max_Count":100
},
{
"item_ID":0,
"Chance_To_Get":1,
"Priority":1,
"Min_Count":1,
"Max_Count":100
}
]
}
]
The question is: how to get an array Reward from a file?
Comment
I fill an array of Classes Reward like this:
for (int i = 0; i < database_$$anonymous$$onsters.Count; i++)
{
database_$$anonymous$$onsters[i].rewards = new Reward[json_monsterData[i]["Reward"].Count];
for (int f = 0; f < database_$$anonymous$$onsters[i].rewards.Length; f++)
{
database_$$anonymous$$onsters[i].rewards[f] = new Reward();
}
for (int j = 0; j < database_$$anonymous$$onsters[i].rewards.Length; j++)
{
database_$$anonymous$$onsters[i].rewards[j].item_ID = (int)json_monsterData[i]["Reward"][j]["item_ID"];
database_$$anonymous$$onsters[i].rewards[j].Chance_To_Get = (int)json_monsterData[i]["Reward"][j]["Chance_To_Get"];
database_$$anonymous$$onsters[i].rewards[j].Priority = (int)json_monsterData[i]["Reward"][j]["Priority"];
database_$$anonymous$$onsters[i].rewards[j].$$anonymous$$in_Count = (int)json_monsterData[i]["Reward"][j]["$$anonymous$$in_Count"];
database_$$anonymous$$onsters[i].rewards[j].$$anonymous$$ax_Count = (int)json_monsterData[i]["Reward"][j]["$$anonymous$$ax_Count"];
}
}
But are there more effective ways to do that? For example if i will have 10 - 20 arrays?
Follow this Question
Related Questions
Get JSON array object string value 2 Answers
jSON data error Unity3d 0 Answers
LitJSON android problem 1 Answer
Distribute terrain in zones 3 Answers
I want to convert image data(Texture2D) to Base64 string and store in JSON file in Unity3D(C#). 0 Answers