Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Endesmomo · Dec 24, 2012 at 03:47 PM · importjsonfiledata

How to read data from a text file into a collection? (JSON preferred)

I need to read several lists of 2D coordinates into a data collection, like a Dictionary for example, from a text file.
I can make the file into any format (it´s originally an SVG but I made an ETL that cleans it and can spit JSON or whatever other format) but JSON is preferred. Thanks!

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
0

Answer by MaGuSware™ · Dec 24, 2012 at 04:46 PM

Well, I do it using FileStream. I first save the length of the array and then do a for loop to save the data.

Here is an example of saving an array on integers in C#

 using System.IO;

 public class SomeClass
 {
     FileStream file;
     
     public void SaveArray(int[] arr)
     {
         file = File.open("myfile.format", FileMode.OpenOrCreate);
         
         byte[] arraySize = System.BitConverter.GetBytes(arr.Length);
         file.Write(arraySize, 0, sizeof(int));
         
         for(int i = 0; i < arr.Length; i++)
         {
             byte[] arrayElement = System.BitConverter.GetBytes(arr[i]);
             file.Write(arrayElement, 0, sizeof(int));
         }
         
         file.Close();
     }
     
     public void LoadArray(int[] arr)
     {
         if (file = File.Open("myfile.format", FileMode.Open))
         {
             byte[] arraySize = new byte[sizeof(int)];
             file.Read(arraySize, 0, sizeof(int));
             
             arr = new int[arraySize];
             
             for (int i = 0; i < arraySize; i++)
             {
                 byte[] arrayElement = new byte[sizeof(int)];
                 file.Read(arrayElement, 0, sizeof(int));
                 
                 arr[i] = System.BitConverter.ToInt32(arrayElement);
             }
             
             file.Close();
         }
     }
 }

You need to remember, when saving, things come out in the exact same way they go in. So since you are doing an array of 2D coordinates. That means in the loop, you would do 2 writes and 2 reads instead of one. One for each float in the coordinate.

 for(int i = 0; i < arr.Length; i++)
 {
     byte[] arrayElement = System.BitConverter.GetBytes(arr[i].x);
     file.Write(arrayElement, 0, sizeof(int));

     arrayElement = System.BitConverter.GetBytes(arr[i].y);
     file.Write(arrayElement, 0, sizeof(int));
 }

The same principles should apply with whatever format you choose to use. You just need to find out how the co-ordinates are stored in the file so you know what datatypes to pull out the data as.

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 Endesmomo · Dec 24, 2012 at 06:12 PM 0
Share

Thanks for the extensive reply. This is for binary files, but I'd like to interpret text files in a format like JSON for example. And I made a mistake, I wrote arrays, but I was thinking rather in a more flexible data collection like Dictionaries.

avatar image MaGuSware™ · Dec 24, 2012 at 06:43 PM 0
Share

What does the data look like in the file

avatar image Endesmomo · Dec 24, 2012 at 07:34 PM 0
Share

It could be any format, but I prefer JSON.

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

9 People are following this question.

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

Related Questions

Serialized files over 4.00 GB (4294967295 bytes) cannot be loaded by the player 1 Answer

"Invalid Data File" error in every server I try (Facebook game) 1 Answer

Can't open json from editor 1 Answer

do something if a method cant be executed 1 Answer

Create a glossary using Simple JSON, loading data dynamically 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