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 /
avatar image
0
Question by carlosmonleon5 · Mar 28, 2020 at 08:37 PM · json

Problem retrieving data from Json to a list.

Hello im using the following script to fill a json with some data from a open source API and when i try to load the data into a List it gives me a "IOException: Sharing violation on path" + my path. And i don't know wat is happening.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 using System.IO;
 
 public class APICall : MonoBehaviour
 {
     readonly string url = "https://opentdb.com/api.php?amount=10&category=10&difficulty=easy&type=multiple";
     public List<QuestionItem> questions_;
 
     
     void Awake()
     {
         StartCoroutine(SimpleWebRequest());
     }
 
     private void Start()
     {
         string path = Application.persistentDataPath + "/" + "questions.json";
         string loadedquestion = JSONUtility.LoadJsonAsExternalResource(path);
         QuestionItem myloadedQuestion = JsonUtility.FromJson<QuestionItem>(loadedquestion);
         questions_.Add(myloadedQuestion);
     }
 
     IEnumerator SimpleWebRequest()
     {
         UnityWebRequest apiRequest = UnityWebRequest.Get(url);
 
         yield return apiRequest.SendWebRequest();
 
         if (apiRequest.isNetworkError || apiRequest.isHttpError)
         {
             Debug.Log(apiRequest.error);
         }
         else
         {
             JSONUtility.WriteJsonToExternalResources("questions.json", apiRequest.downloadHandler.text);
         }
 
     }
 }


QuestionItem Script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class QuestionItem 
 {
     public string question; 
     public string correct_answer;
     public string[] incorrect_answers; 
     public string type;
     public string difficulty;
     public string category;
 }
 

JSONUtilities Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 using System.Text;
 
 public class JSONUtility
 {
    
     public static string LoadJsonFromFile(string path, bool isResource)
     {
         if (isResource)
         {
             return LoadJsonAsResource(path);
         }
         else
         {
             return LoadJsonAsExternalResource(path);
         }
     }
 
    public static string LoadJsonAsResource(string path)
     {
         string jsonFilePath = path.Replace(".json","");
         TextAsset loadedJsonFile = Resources.Load<TextAsset>(jsonFilePath);
         return loadedJsonFile.text;
     }
     
     public static string LoadJsonAsExternalResource(string path)
     {
         
         if (!File.Exists(path))
         {
             Debug.Log("File not found");
             return null;
         }
         StreamReader reader = new StreamReader(path);
         string response = "";
         while (!reader.EndOfStream)
         {
             response += reader.ReadLine();
         }
         return response;
     }
 
     public static void WriteJsonToExternalResources(string path,string content)
     {
         path = Application.persistentDataPath + "/" + path;
         FileStream stream = File.Create(path);
         byte[] contentBytes = new UTF8Encoding(true).GetBytes(content);
         stream.Write(contentBytes,0,contentBytes.Length);
     }
 
 }

That everything im using in this problem.

Thank you for your time.

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
Best Answer

Answer by ShadyProductions · Mar 28, 2020 at 08:54 PM

A shared path violation happens usually when you access a file that has a lock. It probably has to do with the fact you don't actually close your streams.

You have to add a using to your streams, that will make sure they are properly disposed.

 using (FileStream stream = File.Create(path))
 {
 // logic
 }
 
 using (StreamReader reader = new StreamReader(path)) 
 {
 // logic
 }

Also your web requests should be disposed aswel

 using (UnityWebRequest apiRequest = UnityWebRequest.Get(url)) 
 {
 
 }

Also you might run into a race condition incase your coroutine hasn't finished by the time your start function runs, it might not have yet saved the json data.

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.

Follow this Question

Answers Answers and Comments

125 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

Related Questions

How to save and load the state of dialogue when changing scenes 1 Answer

How to save and load any data type? 1 Answer

C# Converting json arrays using JsonUtility, 1 Answer

JSON invalid value 1 Answer

Unity doesn't see files inside StreamingAssets folder 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