• 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 Aziz1989 · Jul 06, 2015 at 02:56 PM · c#unity 5json

How to get a JSON string of a folder structure with c# in Unity3d

Hello,

i am willing to generate a json string containing the structure of a given folder, searching where to start, i found that is easy using .NET but i don't know why i didn't get it in unity, maybe it is obvious, but excuse me for this if so, i am just a beginner. Thank you.

Comment
Add comment · Show 2
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 Nerevar · Jul 06, 2015 at 03:11 PM 0
Share

I don't see how this json would look like, you could say that every folder would be an array of files, names of the folders and files would be keys in the json but what would be their values?

avatar image Aziz1989 · Jul 07, 2015 at 08:08 AM 0
Share

the name of files type i want to fetch, for instance if i am into pngs i will affect the names of png files as list or something...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Aziz1989 · Jul 07, 2015 at 12:47 PM

Hi again,

i managed to code this:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using Pathfinding.Serialization.JsonFx;
 using System.IO;
 
 public class JBrowser : MonoBehaviour
 {
 
     private string title ;
     private bool isFolder ;
     private string key ;
     private List<string> children;
     private  DirectoryInfo fsi;
     private StreamWriter streamWriter;
     private bool on = true;
 
 
     void Start ()
     {
         fsi = new DirectoryInfo (@"c:\");
         title = fsi.Name;
         Debug.Log (title);
         children = new List<string> ();
     }
 
     void Update ()
     {
         if (on) {
             if (fsi.Attributes == FileAttributes.Directory) {
                 isFolder = true;
                 foreach (FileSystemInfo f in fsi.GetFileSystemInfos()) {
                     children.Add (f.Name.ToString ());
                 }
             } else {
                 isFolder = false;
             }
             key = title.Replace (" ", "").ToLower ();
             
             //        JsonToDynatree ();
             //    }
             //        
             //
             //    private void JsonToDynatree ()
             //    {
             string data = JsonWriter.Serialize (children);
             //        if(!Directory.Exists(PATH)){
             //            Directory.CreateDirectory(PATH);
             //        }
             streamWriter = new StreamWriter (@"c:\JsonBrowsingList.txt");
             streamWriter.Write (data);
             streamWriter.Close ();
             //        return JsonConvert.SerializeObject (this, Formatting.Indented);
             on = false;
         }
         
     }
 
 }


and i ve created the json.txt file in the c:/ drive, and it is supposed to be filled with the folders and files names but instead is only showing empty brackets... ?? ([])

Comment
Add comment · Show 1 · 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 karsnen · Apr 10, 2018 at 12:25 PM 0
Share

$$anonymous$$aybe this : https://docs.microsoft.com/en-us/dotnet/csharp/program$$anonymous$$g-guide/file-system/how-to-iterate-through-a-directory-tree

avatar image
0

Answer by Bomanden · Apr 28, 2018 at 11:14 AM

Old question, but maybe others will land here, (Have a look at this post) https://stackoverflow.com/questions/24725775/converting-a-directory-structure-and-parsing-to-json-format-in-c-sharp

Just change out to your preferred parser.

 public string JsonToDynatree()
 {
   //return JsonConvert.SerializeObject(this, Formatting.Indented);
   return JsonUtility.ToJson(this, true);
 }

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can we create new variables or properties from the inspector? 0 Answers

KeyNotFoundException: The given key was not present in the dictionary. 2 Answers

Parsing multiple json files that are referencing each other 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