我需要将.json转换为数组

时间:2019-04-05 02:31:28

标签: c# unity3d

我正在构建一个可以统一管理学校时间表的应用程序。我想使用一个巨大的json文件。我认为将其转换为数组最简单

为膨胀而道歉,虽然这只是我们所拥有的〜1%。

[
  {
    "DayNumber": "2",
    "Period": "4",
    "ClassCode": "9HM.A2",
    "TeacherCode": "YOUB",
    "RoomCode" : "205",
    "ClassTime" : "12:40"
  },
  {
    "DayNumber": "3",
    "Period": "2",
    "ClassCode": "9HM.A2",
    "TeacherCode": "YOUB",
    "RoomCode" : "205",
    "ClassTime" : "10:00"
  },
  {
    "DayNumber": "5",
    "Period": "3",
    "ClassCode": "9HM.A2",
    "TeacherCode": "YOUB",
    "RoomCode" : "205",
    "ClassTime" : "11:08"
  },
  {
    "DayNumber": "8",
    "Period": "3",
    "ClassCode": "9HM.A2",
    "TeacherCode": "YOUB",
    "RoomCode" : "205",
    "ClassTime" : "11:20"
  },
  {
    "DayNumber": "10",
    "Period": "3",
    "ClassCode": "9HM.A2",
    "TeacherCode": "YOUB",
    "RoomCode": "205",
    "ClassTime": "11:08"
  },
  {
    "DayNumber": "1",
    "Period": "6",
    "ClassCode": "9IST.B1",
    "TeacherCode": "FRWK",
    "RoomCode" : "166",
    "ClassTime" : "14:27"
  },
  {
    "DayNumber": "4",
    "Period": "3",
    "ClassCode": "9IST.B1",
    "TeacherCode": "FRWK",
    "RoomCode" : "166",
    "ClassTime" : "11:40"
  },
  {
    "DayNumber": "5",
    "Period": "6",
    "ClassCode": "9IST.B1",
    "TeacherCode": "FRWK",
    "RoomCode" : "166",
    "ClassTime" : "14:27"
  },
  {
    "DayNumber": "6",
    "Period": "6",
    "ClassCode": "9IST.B1",
    "TeacherCode": "FRWK",
    "RoomCode" : "166",
    "ClassTime" : "14:27"
  },
  {
    "DayNumber": "9",
    "Period": "3",
    "ClassCode": "9IST.B1",
    "TeacherCode": "FRWK",
    "RoomCode" : "166",
    "ClassTime" : "11:40"
  },
  {
    "DayNumber": "2",
    "Period": "5",
    "ClassCode": "9SC.5",
    "TeacherCode": "TRUC",
    "RoomCode" : "27",
    "ClassTime" : "14:20"
  },
  {
    "DayNumber": "3",
    "Period": "3",
    "ClassCode": "9SC.5",
    "TeacherCode": "TRUC",
    "RoomCode" : "27",
    "ClassTime" : "11:20"
  },
  {
    "DayNumber": "4",
    "Period": "2",
    "ClassCode": "9SC.5",
    "TeacherCode": "TRUC",
    "RoomCode" : "27",
    "ClassTime" : "10:00"
  },
  {
    "DayNumber": "5",
    "Period": "4",
    "ClassCode": "9SC.5",
    "TeacherCode": "TRUC",
    "RoomCode" : "27",
    "ClassTime" : "12:01"
  },
  {
    "DayNumber": "7",
    "Period": "5",
    "ClassCode": "9SC.5",
    "TeacherCode": "TRUC",
    "RoomCode" : "27",
    "ClassTime" : "14:20"
  },
  {
    "DayNumber": "8",
    "Period": "2",
    "ClassCode": "9SC.5",
    "TeacherCode": "TRUC",
    "RoomCode" : "27",
    "ClassTime" : "10:00"
  },
  {
    "DayNumber": "9",
    "Period": "2",
    "ClassCode": "9SC.5",
    "TeacherCode": "TRUC",
    "RoomCode" : "27",
    "ClassTime" : "10:00"
  },
  {
    "DayNumber": "10",
    "Period": "4",
    "ClassCode": "9SC.5",
    "TeacherCode": "TRUC",
    "RoomCode" : "27",
    "ClassTime" : "12:01"
  }
]

我想将其自动转换为该文件,甚至想出如何在c#中处理上述文件

string[][] _9HM_A2 = new string[][]
{
    new string[] {"2", "4", "9HM.A2", "YOUB", "205", "12:40"},
    new string[] {"3", "2", "9HM.A2", "YOUB", "205", "10:00"},
    new string[] {"5", "3", "9HM.A2", "YOUB", "205", "11:08"},
    new string[] {"8", "3", "9HM.A2", "YOUB", "205", "11:20"},
    new string[] {"10", "3", "9HM.A2", "YOUB", "205", "11:08"},
};
    string[][] _9IST_B1 = new string[][]
{
    new string[] { "1", "6", "9IST.B1", "FRWK", "166", "14:27"},
    new string[] {"4", "3", "9IST.B1", "FRWK", "166", "11:40"},
    new string[] {"5", "6", "9IST.B1", "FRWK", "166", "14:27"},
    new string[] {"6", "6", "9IST.B1", "FRWK", "166", "14:27"},
    new string[] {"9", "3", "9IST.B1", "FRWK", "166", "11:40"},
};
    string[][] _9SC_5 = new string[][]
{
    new string[] { "2", "5", "9SC.5", "TRUC", "27", "14:20"},
    new string[] { "3", "3", "9SC.5", "TRUC", "27", "11:20"},
    new string[] { "4", "2", "9SC.5", "TRUC", "27", "10:00"},
    new string[] { "5", "4", "9SC.5", "TRUC", "27", "12:01"},
    new string[] { "7", "5", "9SC.5", "TRUC", "27", "14:20"},
    new string[] { "8", "2", "9SC.5", "TRUC", "27", "10:00"},
    new string[] { "9", "2", "9SC.5", "TRUC", "27", "10:00"},
    new string[] { "10", "4", "9SC.5", "TRUC", "27", "12:01"},
};

1 个答案:

答案 0 :(得分:0)

如果这是一个巨大的json文件,那么应该考虑使用liteweight数据库而不是数组。如果需要对数据进行聚合,则数据库可能更合适。将巨大的json文件加载到数组中可能会耗尽您的内存资源。但是,如果您仍然想尝试一个数组,为什么不使用列表字典呢?

根据您的示例json,我的假设是该json的结构不会改变,因此您可以将c#POCO类映射到该数据。

public class ClassSched
    {
        public string DayNumber { get; set; }
        public string Period { get; set; }
        public string ClassCode { get; set; }
        public string TeacherCode { get; set; }
        public string RoomCode { get; set; }
        public string ClassTime { get; set; }
    }

这是我可能要如何存储此数据的摘要。

Dictionary<string, List<ClassSched>> dictClassSched = new Dictionary<string, List<ClassSched>>();

/*
    * Here you open up your Json file and read it,
    * then you loop over the rows
    * and grab the class Code and assign it to classCode variable
    * and assign the row of the json into the jsonRow variable.
    * 
    */

var c = JsonConvert.DeserializeObject<ClassSched>(jsonrow.ToString());
if ( dictClassSched.TryGetValue(classCode, out List<ClassSched> scheds))
{
    scheds.Add(c);
    dictClassSched[classCode] = scheds;
}
else
{
    dictClassSched.Add(classCode, new List<ClassSched>() { c });
}

因此,在上方,我们将json行反序列化为POCO。然后我们将classCode放在字典中查找,如果找到匹配项,则将使用内容填充scheds变量。然后,我们插入新行,然后将列表设置回字典。如果找不到该类代码的条目,则创建一个新的List并为其分配classSched实例。

-HTH