【发布时间】:2012-10-29 03:39:39
【问题描述】:
我花了两天时间“花时间”处理代码示例等,试图将一个非常大的 JSON 文件读入 c# 中的一个数组,以便以后可以将其拆分为一个二维数组进行处理。
我遇到的问题是我找不到任何人在做我想做的事情的例子。这意味着我只是在编辑代码,希望能取得最好的结果。
我已经设法让一些工作:
- 读取文件遗漏标头,仅将值读取到数组中。
- 在数组的每一行放置一定数量的值。 (所以我 以后可以将其拆分为二维数组)
这是使用下面的代码完成的,但是在向数组中输入几行后它会导致程序崩溃。这可能与文件大小有关。
// If the file extension was a jave file the following
// load method will be use else it will move on to the
// next else if statement
if (fileExtension == ".json")
{
int count = 0;
int count2 = 0;
int inOrOut = 0;
int nRecords=1;
JsonTextReader reader = new JsonTextReader(new StreamReader(txtLoaction.Text));
string[] rawData = new string[5];
while (reader.Read())
{
if (reader.Value != null)
if (inOrOut == 1)
{
if (count == 6)
{
nRecords++;
Array.Resize(ref rawData, nRecords);
//textBox1.Text += "\r\n";
count = 0;
}
rawData[count2] += reader.Value + ","; //+"\r\n"
inOrOut = 0;
count++;
if (count2 == 500)
{
MessageBox.Show(rawData[499]);
}
}
else
{
inOrOut = 1;
}
}
}
我正在使用的 JSON 的 sn-p 是:
[
{ "millis": "1000",
"stamp": "1273010254",
"datetime": "2010/5/4 21:57:34",
"light": "333",
"temp": "78.32",
"vcc": "3.54" },
]
我需要这个 JSON 中的值。例如,我需要“3.54”,但我不希望它打印“vcc”。
我希望有人能告诉我如何读取 JSON 文件并只提取我需要的数据并将其放入数组或稍后我可以使用的东西放入数组中。
【问题讨论】:
-
你的程序崩溃时会抛出什么异常?
-
这能回答你的问题吗? How can I parse JSON with C#?
标签: c# json parsing large-files