• 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 · stringtimerjsontimespan

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/mins 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, minutes and seconds? Or is there any other way of getting the hours/minutes/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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

System.DateTime truncate Milliseconds 1 Answer

A string converted from Json cant become "smart" and understand variables in my code, true or false? 1 Answer

JSON Saving/loading " error CS0029: Cannot implicitly convert type 'void' to 'string' " 1 Answer

how to Subtract string from TimeSpan 0 Answers

Stop working after for loop appending string 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges