Я не являюсь перед компьютером с C# прямо сейчас, поэтому я не могу полностью попробовать это.
Что вы собираетесь нужно сделать @Jakob следующий:
- Написать парсер, который будет загрузить файл и сохранить его в памяти.
- Для каждого раздела, который вы хотите «разбор» в массиве C# (например,
zonearray
), вам необходимо настроить границы, чтобы начать поиск и завершить поиск файла. Пример: Мы знаем, что zonearray
начинает строить массив двумя линиями после zonearray[i] = new Array(1);
и заканчивается на zonearray.sort()
.
- Таким образом, с этими ограничениями мы можем прокручивать каждую строку между синтаксисом и анализировать массив C#. Это достаточно просто, я думаю, что вы можете понять. Вам также нужно будет помнить о субиндексе.
- Повторите это 2-3 для каждого массива, который вы хотите проанализировать (
zonearray
, roomarray
..etc).
Если вы не можете достаточно выяснить как закодировать границы или как разобрать строку и складывать их в массивы, я мог бы написать что-то завтра (даже если это праздник в Канаде).
EDIT: Следует отметить, что для этого нельзя использовать какой-либо JSON-синтаксический анализатор; вы должны написать свой собственный. На самом деле это не так сложно сделать, вам просто нужно разбить его на небольшие шаги (сначала выясните, как прошивать каждую строку и находить правильные «границы»).
НТН
EDIT: Я только что провел ~ 20 минут, пишущие это для вас. Он должен разобрать файл и загрузить каждый массив в List<string[]>
. Я сильно прокомментировал это, чтобы вы могли видеть, что происходит. Если у вас есть вопросы, не стесняйтесь спрашивать. Ура!
private class SearchBound
{
public string ArrayName { get; set; }
public int SubArrayLength { get; set; }
public string StartBound { get; set; }
public int StartOffset { get; set; }
public string EndBound { get; set; }
}
public static void Main(string[] args)
{
//
// NOTE: I used FireFox to determine the encoding that was used.
//
List<string> lines = new List<string>();
// Step 1 - Download the file and dump all the lines of the file to the list.
var request = WebRequest.Create("http://skema.ku.dk/life1011/js/filter.js");
using (var response = request.GetResponse())
using(var stream = response.GetResponseStream())
using(var reader = new StreamReader(stream, Encoding.GetEncoding("ISO-8859-1")))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line.Trim());
}
Console.WriteLine("Download Complete.");
}
var deptArrayBounds = new SearchBound
{
ArrayName = "deptarray", // The name of the JS array.
SubArrayLength = 2, // In the JS, the sub array is defined as "new Array(X)" and should always be X+1 here.
StartBound = "deptarray[i] = new Array(1);",// The line that should *start* searching for the array values.
StartOffset = 1, // The StartBound + some number line to start searching the array values.
// For example: the next line might be a '}' so we'd want to skip that line.
EndBound = "deptarray.sort();" // The line to stop searching.
};
var zoneArrayBounds = new SearchBound
{
ArrayName = "zonearray",
SubArrayLength = 2,
StartBound = "zonearray[i] = new Array(1);",
StartOffset = 1,
EndBound = "zonearray.sort();"
};
var staffArrayBounds = new SearchBound
{
ArrayName = "staffarray",
SubArrayLength = 3,
StartBound = "staffarray[i] = new Array(2);",
StartOffset = 1,
EndBound = "staffarray.sort();"
};
List<string[]> deptArray = GetArrayValues(lines, deptArrayBounds);
List<string[]> zoneArray = GetArrayValues(lines, zoneArrayBounds);
List<string[]> staffArray = GetArrayValues(lines, staffArrayBounds);
// ... and so on ...
// You can then use deptArray, zoneArray etc where you want...
Console.WriteLine("Depts: " + deptArray.Count);
Console.WriteLine("Zones: " + zoneArray.Count);
Console.WriteLine("Staff: " + staffArray.Count);
Console.ReadKey();
}
private static List<string[]> GetArrayValues(List<string> lines, SearchBound bound)
{
List<string[]> values = new List<string[]>();
// Get the enumerator for the lines.
var enumerator = lines.GetEnumerator();
string line = null;
// Step 1 - Find the starting bound line.
while (enumerator.MoveNext() && (line = enumerator.Current) != bound.StartBound)
{
// Continue looping until we've found the start bound.
}
// Step 2 - Skip to the right offset (maybe skip a line that has a '}').
for (int i = 0; i <= bound.StartOffset; i++)
{
enumerator.MoveNext();
}
// Step 3 - Read each line of the array.
while ((line = enumerator.Current) != bound.EndBound)
{
string[] subArray = new string[bound.SubArrayLength];
// Read each sub-array value.
for (int i = 0; i < bound.SubArrayLength; i++)
{
// Matches everything that is between an equal sign then the value
// wrapped in quotes ending with a semi-colon.
var m = Regex.Matches(line, "^(.* = \")(.*)(\";)$");
// Get the matched value.
subArray[i] = m[0].Groups[2].Value;
// Move to the next sub-item if not the last sub-item.
if (i < bound.SubArrayLength - 1)
{
enumerator.MoveNext();
line = enumerator.Current;
}
}
// Add the sub-array to the list of values.
values.Add(subArray);
// Move to the next line.
if (!enumerator.MoveNext())
{
break;
}
}
return values;
}
Вы хотите сказать, что массив находится в формате JSON? Это может помочь, если вы покажете небольшой пример того, что у вас есть. –
Используйте свой редактор, чтобы превратить квадратные скобки в фигурные скобки. –
, к сожалению, не отформатирован JSON, это массив javascript. Я отредактировал основную запись, чтобы привести пример – Jakob