我终于解析了所有解析工作,只有一个例外。
以下代码完全按照原样运作
public void GetCurrentSchedule()
{
String JSONstring = File.ReadAllText("\\USER\\Schedule\\Schedule.txt");
RootObject p1 = JsonConvert.DeserializeObject<RootObject>(JSONstring);
for (int a = i; a <= (i + 3); a++)
{
sDay = p1.schedulePeriods[a].day;
sPeriod = p1.schedulePeriods[a].periodType;
sStart = p1.schedulePeriods[a].startTime;
sCancel= p1.schedulePeriods[a].isCancelled;
sHeat = p1.schedulePeriods[a].heatSetpoint;
sCool = p1.schedulePeriods[a].coolSetpoint;
sFan = p1.schedulePeriods[a].fanMode;
Console.PrintLine("day: {0}", sDay);
Console.PrintLine("period: {0}", sPeriod);
Console.PrintLine("start: {0}", sStart);
Console.PrintLine("Cancel: {0}", sCancel);
Console.PrintLine("Heat: {0}", sHeat);
Console.PrintLine("Cool: {0}", sCool);
Console.PrintLine("Fan: {0}", sFan);
}
}
然而,我想要做的是将每个传递分配给稍后按索引号调用的数组。
例如
sDay = p1.schedulePeriods[a].day;
我希望有4个sDay索引,我可以通过另一个类的索引号来调用。
我一直在撞墙试图让这个工作。
有什么想法吗?
答案 0 :(得分:1)
我相信你想要这样的东西?
public void GetCurrentSchedule()
{
String JSONstring = File.ReadAllText("\\USER\\Schedule\\Schedule.txt");
RootObject p1 = JsonConvert.DeserializeObject<RootObject>(JSONstring);
string[] sDay = new string[i + 4];//Declare your array here (+4 becuase your for loop goes upto +3)
for (int a = i; a <= (i + 3); a++)
{
sDay[a - i] = p1.schedulePeriods[a].day; // assign the value to array element (a - i because if i > 0 because arrays start with 0)
sPeriod = p1.schedulePeriods[a].periodType;
sStart = p1.schedulePeriods[a].startTime;
sCancel= p1.schedulePeriods[a].isCancelled;
sHeat = p1.schedulePeriods[a].heatSetpoint;
sCool = p1.schedulePeriods[a].coolSetpoint;
sFan = p1.schedulePeriods[a].fanMode;
Console.PrintLine("day: {0}", sDay[a - i]); // Call it with index
Console.PrintLine("period: {0}", sPeriod);
Console.PrintLine("start: {0}", sStart);
Console.PrintLine("Cancel: {0}", sCancel);
Console.PrintLine("Heat: {0}", sHeat);
Console.PrintLine("Cool: {0}", sCool);
Console.PrintLine("Fan: {0}", sFan);
}
}
答案 1 :(得分:1)
public List<string> SDays = new List<string>(); // or whatever type
// inside method
String JSONstring = File.ReadAllText("\\USER\\Schedule\\Schedule.txt");
RootObject p1 = JsonConvert.DeserializeObject<RootObject>(JSONstring);
for (int a = i; a <= (i + 3); a++)
{
SDays.Add(p1.schedulePeriods[a].day);
//and so on
}
致电SDays[index]
以从其他班级获得正确的一天
您可能也希望将其更改为更清晰的OOP样式。