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 /
  • Help Room /
avatar image
0
Question by WHATS_THAT · Oct 01, 2015 at 05:19 AM · c#listlitjson

Extract data from a litjson file and load it to a list C#

Sorry this is a repost, I accidentally erased my other one. I'm a total noob on here, but alright at unity engine. Anyway I was wonder how would I go about extracting data from a litjson save file (Where I saved data from one list) , and load it in another list. Thanks.

  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  
  [System.Serializable]
  public class JsonIcons {
      
      public string IconName;//Shows the icon Name in the list
      public int IconID;// Shows the Icon ID in the list
      public Sprite AssignIcon;
  
      
      public JsonIcons(string Name, int ID )
      {
  
          IconName = Name;
          IconID = ID;
      }
      
  
      public JsonIcons()
      {
          
          
      }
      
      
  
  }
  
  
  
  
  
  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  using LitJson;
  using System.IO;
  
  //This class that does the saving
  public class JsonTest : MonoBehaviour {
  
      public List<JsonIcons> JIcon = new List<JsonIcons>();
      public JsonData JCD;
  
      protected JsonIcons KZ,
      TestTK;
  
      
      
      public void Start()
      {
          TestTK = new JsonIcons("Kagami", 40);
          KZ = new JsonIcons("Magic", 0);
          
          JIcon.Add(TestTK); //Add things to the list to be save
          JIcon.Add(KZ);
  
          JCD = JsonMapper.ToJson(JIcon);
          //This is where I saved the things inside the JIcon list to a Json file
          File.WriteAllText(Application.dataPath + "/JsonSaveTest.json", JCD.ToString());
          //Debug.Log(JCD);
  
      }
  
  }
  
  
  
  
  
  
  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  using System.IO;
  using LitJson;
  
  
  public class JsonReadTest : MonoBehaviour {
  
      public List<JsonIcons> ReadSJ = new List<JsonIcons>();
      
      private string JString;
      public JsonData IconData;
  
      // Use this for initialization
      void Start () 
      {
          //Trying to get this file to load in the correct format inside the ReadSJ list
          JString = File.ReadAllText(Application.dataPath + "/JsonFiles/JsonSaveTest.json");
          IconData = JsonMapper.ToObject(JString);
      }
  
  
  
      // Update is called once per frame
      void Update () 
      {
      
      }
  }
  
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by GAMZER0 · Oct 01, 2015 at 05:43 AM

Hey what's up I was looking for your old post, now I see why I couldn't find it. I believe I have something that could steer you in the right way, as it doesn't convert the whole list at once just only one element. But as I was messing around I came up with something like this. Maybe someone has a better way. But this is what I got:

 using UnityEngine;
 using System; //Need this for the convert.Int32
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using LitJson;
 
 
 public class JsonReadTest : MonoBehaviour {
 
     public List<JsonIcons> ReadSJ = new List<JsonIcons>();
 
     public JsonIcons JI;
     private string JString;
     public JsonData IconData;
 
 
     //public JsonIcons[] ExampleTest = Directory.GetFiles(Application.dataPath );
 
     // Use this for initialization
     void Start () 
     {    JString = File.ReadAllText(Application.dataPath + "/JsonFiles/JsonSaveTest.json");
         IconData = JsonMapper.ToObject(JString);
 
         JI = new JsonIcons();// This helps to add things to this list
         JI.IconName = IconData[0]["IconName"].ToString(); //This add the Iconname to the list
         JI.IconID = Convert.ToInt32( IconData[0]["IconID"].ToString());//This converts the number to be used as a string to the list
 
         Debug.Log(JString);
         ReadSJ.Add(JI); //And this adds the things inside of "new JsonIcons()"
 
 
     }
 }



 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;//You don't need this I was just testing
 using LitJson;//You don't need this I was just testing
 
 [System.Serializable]
 public class JsonIcons {
     
     public string IconName;
     public int IconID;
     public Sprite AssignIcon;
 
     
     public JsonIcons(string Name, int ID )
     {
 
         IconName = Name;
         IconID = ID;
     }
 
 
 }
Comment
Add comment · Show 3 · Share
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 WHATS_THAT · Oct 01, 2015 at 06:07 AM 0
Share

Thanks a lot dude!!!!!! I really really Appreciate it. This will help me out a lot. :-)

avatar image GAMZER0 WHATS_THAT · Oct 01, 2015 at 06:14 AM 1
Share

No prob my friend, I glad I could of help. I know how annoying it is to get stuck. You could probably use a for loop or something to loop through and add things to the list, and to clean things up. Ins$$anonymous$$d of adding them in one by one, because that would be very time consu$$anonymous$$g, and a bunch of unnecessary code.

avatar image WHATS_THAT GAMZER0 · Oct 01, 2015 at 06:15 AM 0
Share

That's a very good idea I was thinkn along the lines of for loop.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 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

Related Questions

Problem when acessing a list from another script? (ArgumentOutOfRangeException) 0 Answers

Is there anyway to make a list of prewritten variables? (C#) 2 Answers

Insert string into empty list at a specific index 0 Answers

How to get all children of a Gameobject with a certain component 2 Answers

ArgumentOutOfRangeException: Argument is out of range. 1 Answer


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