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 inigopanos · Dec 17, 2020 at 10:25 AM · timerstringjsontimespan

How can I send a TimeSpan value to a Json file?

So I'm trying to record how long it takes for someone to complete doing one action. To have an accurate measurement, I'm using TimeSpan to get hours, minutes and seconds. I also have a script where I send various measurements and variables to a json file to be saved on the device, but I can't send the TimeSpan , and the Json doesn't even show the variable even tho it is declared. I tried to convert the TimeSpan into a string but it didn't work, so I'm probably doing something wrong, but I don't know what. Any help would be appreciated.

This is the TimeSpan recording script, with my attemprs at turning it into a string:

 if  (!completadoPaso1 && !completadoPaso2)
         {
             Debug.Log("Comienza el contador primer paso");
             this.timerPaso1 += Time.deltaTime;
             timeSpan_1 = TimeSpan.FromSeconds(timerPaso1);
             string pasoTiempo_1 = string.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan_1.Hours, timeSpan_1.Minutes, timeSpan_1.Seconds);
 
             pasoTiempo_1.ToString();
             //print(timeSpan_1);
 
             Data.sharedInstance.datos.EJ_tT1 = timeSpan_1;
             Data.sharedInstance.ReescribirArchivo();
         }

This is the timeSpan variable, created in a [System.Serializable] class on another script.

 public TimeSpan EJ_tT1;

And this is the Json file. The EJ_tT1 should be in between those two values. The first EJ_T shows because it is a float, and EJ_nbote is an integer.

 "EJ_t":1.3730500936508179,"EJ_nbote":3,


Any help is appreciated.

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 xxmariofer · Dec 17, 2020 at 11:02 AM

Hello, first you can simply delete this line is doing nothing

              pasoTiempo_1.ToString();

second, I imagine you have a class to parse the Json, isnt simplier to add a String to that class called pasoTiempo_1, add the value to the object and simply parse the object?

Comment
Add comment · Show 5 · 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 inigopanos · Dec 17, 2020 at 11:17 AM 0
Share

If I understood correctly, you mean to pass the string value into the json class script and then turn it into a TimeSpan? Problem is, I can't parse the string into a TimeSpan because the Json doesn't read the TimeSpan for some reason. So what I need to do is the opposite, convert the TImeSpan into a string on the json parse script which looks something like this.

  public void ReescribirArchivo()
     {
         jsonString = JsonUtility.ToJson(datos);
         File.WriteAllText(filePath, jsonString);
     }
 
     public void Update()
     {
         //Data.sharedInstance.datos.EJ_tT1 = $$anonymous$$anager.sharedInstance.timeSpan_1;
         //Data.sharedInstance.datos.EJ_tT1.ToString();
         //print(sharedInstance.datos.EJ_tT1);
 
         sharedInstance.datos.EJ_tT1 = TimeSpan.Parse(sharedInstance.datos.pruebaTiempo);       
     }

 
avatar image xxmariofer inigopanos · Dec 17, 2020 at 11:25 AM 0
Share

not every object is serializable, if Im not wrong, you are just creating a TimeSpan using current deltatime, right? you can parse the deltaTime instead into the json, there is no need to parse the full TimeSpan object, or even if you only need some attributes like seconds/hours/$$anonymous$$s you could parse those floats as variables

avatar image inigopanos xxmariofer · Dec 17, 2020 at 11:36 AM 0
Share

So basically you are suggesting I break down the TImeSpans into 3 floats, for hours, $$anonymous$$utes and seconds? Or is there any other way of getting the hours/$$anonymous$$utes/seconds? I don't know a lot about Json and parsing json, it's the first time I have to work with this, so I don't really know how I could take the timers inside my level and send them on to the Json. I have 25 timers I need to send to the json, so breaking them down into three floats each would give me 75 variables for timers alone.

Show more comments

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

144 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

Related Questions

Litjson autoformat 1 Answer

Subtract milliseconds from 12 hours? 1 Answer

Laptime timer using TimeSpan - force milliseconds to 00 and not 000? 1 Answer

JSON Seriallize facebook name 1 Answer

Get JSON array object string value 2 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