在 C# 中读取和解析 Json 文件 [英] Read and parse a Json File in C#
问题描述
我花了两天的大部分时间来处理"代码示例等,试图将一个非常大的 JSON 文件读入 c# 中的数组,以便稍后将其拆分为二维数组进行处理.
我遇到的问题是我找不到任何人做我想做的事情的例子.这意味着我只是在编辑代码,希望能达到最好的效果.
我已经设法使一些工作能够:
- 读取文件错过标题,只将值读入数组.
- 在数组的每一行上放置一定数量的值.(所以我稍后可以将其拆分为二维数组)
这是使用下面的代码完成的,但在向数组中输入几行后它会导致程序崩溃.这可能与文件大小有关.
//如果文件扩展名为 jave 文件,则以下//将使用 load 方法,否则它将移至//下一个 else if 语句如果(文件扩展名 ==.json"){整数计数 = 0;int count2 = 0;int inOrOut = 0;int nRecords=1;JsonTextReader reader = new JsonTextReader(new StreamReader(txtLoaction.Text));字符串[] rawData = 新字符串[5];而 (reader.Read()){if (reader.Value != null)如果 (inOrOut == 1){如果(计数== 6){nRecords++;Array.Resize(ref rawData, nRecords);//textBox1.Text += "
";计数 = 0;}rawData[count2] += reader.Value + ",";//+"
"输入或输出 = 0;计数++;如果(计数 2 == 500){MessageBox.Show(rawData[499]);}}别的{输入或输出 = 1;}}}
我正在使用的 JSON 片段是:
<预><代码>[{毫秒":1000","邮票": "1273010254","日期时间": "2010/5/4 21:57:34","光": "333",温度":78.32","vcc": "3.54" },]我需要这个 JSON 中的值.例如,我需要3.54",但我不希望它打印vcc".
我希望有人能告诉我如何读取 JSON 文件,并且只提取我需要的数据并将其放入数组或稍后我可以用来放入数组的内容.
如何使用 Json.NET一个>?
public void LoadJson(){使用 (StreamReader r = new StreamReader(file.json")){字符串 json = r.ReadToEnd();列表<项目>items = JsonConvert.DeserializeObject>(json);}}公共类项目{公共整数毫秒;公共字符串邮票;公共日期时间日期时间;公共灯串;公众浮动温度;公众流通股vcc;}
您甚至可以在不声明Item
类的情况下动态
获取值.
动态数组 = JsonConvert.DeserializeObject(json);foreach(数组中的变量项){Console.WriteLine({0} {1}", item.temp, item.vcc);}
I have spent the best part of two days "faffing" about with code samples and etc., trying to read a very large JSON file into an array in c# so I can later split it up into a 2d array for processing.
The problem I was having was I could not find any examples of people doing what I was trying to do. This meant I was just editing code a little an hoping for the best.
I have managed to get something working that will:
- Read the file Miss out headers and only read values into array.
- Place a certain amount of values on each line of an array. (So I could later split it an put into 2d array)
This was done with the code below but it crashes the program after entering a few lines into the array. This might have to do with the file size.
// 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 += "
";
count = 0;
}
rawData[count2] += reader.Value + ","; //+"
"
inOrOut = 0;
count++;
if (count2 == 500)
{
MessageBox.Show(rawData[499]);
}
}
else
{
inOrOut = 1;
}
}
}
A snippet of the JSON I am working with is:
[
{ "millis": "1000",
"stamp": "1273010254",
"datetime": "2010/5/4 21:57:34",
"light": "333",
"temp": "78.32",
"vcc": "3.54" },
]
I need the values out of this JSON. For example, I need "3.54", but I would not want it to print the "vcc".
I am hoping someone can show me how to read a JSON file in and only extract the data that I need and put it into an array or something that I can use to later put into an array.
How about making everything easier with Json.NET?
public void LoadJson()
{
using (StreamReader r = new StreamReader("file.json"))
{
string json = r.ReadToEnd();
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);
}
}
public class Item
{
public int millis;
public string stamp;
public DateTime datetime;
public string light;
public float temp;
public float vcc;
}
You can even get the values dynamically
without declaring Item
class.
dynamic array = JsonConvert.DeserializeObject(json);
foreach(var item in array)
{
Console.WriteLine("{0} {1}", item.temp, item.vcc);
}
这篇关于在 C# 中读取和解析 Json 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!