- Home /
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.
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?
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);
}
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
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.
Your answer
Follow this Question
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