Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • 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

avatar image
0
Question by $$anonymous$$ · Dec 21, 2016 at 05:50 PM · c#stringjsonfilelitjson

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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Dec 21, 2016 at 11:55 PM 0
Share

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?

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

260 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges