将列表保存为json格式,DateTime作为标题



我不知道我是否解释得对,但我想要的是能够保存3个字符串列表,但根据时间将它们分开。这个想法是为每一天存储一些字符串,并将它们保存到json中,以便我可以搜索日期并将它们显示到日历中。

My try was like so

[Serializable]
public class TimeStamp
{
public string CreatedDate;
public List<Entries> day = new List<Entries>();
}
[Serializable]
public class Entries
{
public List<string> names = new List<string>();
public List<string> location = new List<string>();
public List<string> info = new List<string>();
}

然后像这样保存

string json = JsonUtility.ToJson(timeStamp);
try
{
timeStamp.CreatedDate = DateTime.UtcNow.ToLocalTime().ToString("dd-MM-yyyy   HH:mm");
File.WriteAllText(Application.persistentDataPath + "/Journal.json", json);
Debug.Log("Data Saved");
}
catch (Exception e)
{
Debug.Log("Error Saving Data:" + e);
throw;
}

这确实保存了我需要的一切,但它不是由日期分开,试图解释它的方式,我认为它是我想要的日期"头"的Json,所以在一个Json文件中,我将有每天的所有条目,我也把它们变成一个列表,所以我可以添加或删除字符串。

所以以后当我想添加一些东西时,我会搜索日期并访问名称列表,例如。add()我的字符串到该列表

在一个相当粗糙的方式,这是我希望我的json看起来像:
entries 
[
"date"[
list1 = []
list2 = []
list3 = []
]          
]

我觉得应该是这样的

[Serializable]
public class Roots
{
public TimeStamp timeStamp;
}
[Serializable]
public class TimeStamp
{
public List<Entries> entries;
}
[Serializable]
public class Entries
{
public List<string> names = new List<string>();
public List<string> location = new List<string>();
public List<string> info = new List<string>();
}

给出如下json

{
"Entries": {
"Timestamp": [
{
"names": "",
"location": "",
"info": ""
}
]
}
}

但我怎么能使时间戳是一个字符串或日期?

我猜你实际上更希望有一个像

这样的集合类
[Serializable] 
public class Root 
{ 
public List<TimeStamp> timestamps = new List<TimeStamp>();
}

,而不是将您的个人时间戳添加到该列表中,例如

// Probably you would load this from JSON at start of your app
private Root root = new Root();
...
root.timeStamps.Add(new TimeStamp(...));

string json = JsonnUtility.ToJson(root);
...


一样
[Serializable]
public class TimeStamp
{
public string CreatedDate;
public List<Entries> day = new List<Entries>();
}
[Serializable]
public class Entries
{
public List<string> names = new List<string>();
public List<string> location = new List<string>();
public List<string> info = new List<string>();
}
这应该会给你一个JSON,看起来像
{
"timestamps" : 
[
{
"CreatedDate" : "12-04-2021   12:34",
"day" : 
[
{
"names" : ["Guy1", "Dude2", "Jack3"],
"location" : ["Room1", "Place2", "Location3"],
"info" : ["This", "is", "some", "info"]
},
{
"names" : [...],
"location" : [...],
"info" : [...]
},
...
]
},
{
"CreatedDate" : "13-07-2021   14:42",
"day" : 
[
{
"names" : [...],
"location" : [...],
"info" : [...]
},
...
]
},
...
]
}

如果你的目标是拥有像

这样的东西
{
"timestamps" : 
[
{
"12-04-2021   12:34": 
{
[
{
"names" : ["Guy1", "Dude2", "Jack3"],
"location" : ["Room1", "Place2", "Location3"],
"info" : ["This", "is", "some", "info"]
},
...
]
}
},
{
"13-07-2021   14:42" :
{
[
{
"names" : [...],
"location" : [...],
"info" : [...]
},
...
]
}
},
...
]
}

则需要使用

[Serializable]
public class Root
{
public Dictionary<string, List<Entries>> timestamps = new Dictionary<string, List<Entries>>();
}
[Serializable]
public class Entries
{
public List<string> names = new List<string>();
public List<string> location = new List<string>();
public List<string> info = new List<string>();
}

,但这是不支持内置的JsonUtility,你宁愿需要使用例如Newtonsoft JSON.NET(可作为一个Unity包),并看到,特别是序列化字典

最新更新