阅读和分析在C#中的JSON文件 [英] Read and parse a Json File in C#

查看:129
本文介绍了阅读和分析在C#中的JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了两天faffing关于与code样品等最好的部分,试图读取一个非常大的JSON文件到C#中的数组,所以我以后可以把它分解成一个二维数组处理

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.

我遇到的问题是我找不到人在做什么,我试图做任何的例子。这意味着我只是编辑code一点点的希望是最好的。

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:


  • 读取文件错过头和只读值到数组中。

  • 将一定量的值上的阵列的每一行。 (所以我
    以后可以把它分解的投产二维数组)

这与下面的code做,但它崩溃输入几行到数组后程序。这可能与文件大小做

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 += "\r\n";
                    count = 0;
                }
                rawData[count2] += reader.Value + ","; //+"\r\n"
                inOrOut = 0;
                count++;
                if (count2 == 500)
                {
                    MessageBox.Show(rawData[499]);
                }
            }
            else
            {
                inOrOut = 1;
            }
    } 
}

我一起工作的JSON的一个片段是:

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" }, 
] 

我所需要的值超出这个JSON的。例如,我需要3.54,但我不希望它打印VCC。

I need the values out of this JSON. For example, I need "3.54", but I would not want it to print the "vcc".

我希望有人能告诉我如何读取一个JSON文件中,只提取我需要的数据,并把它变成一个数组或东西,我可以用它来后放入数组中。

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 all the things easier?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆