如何在c#中读取一个非常大的JSON文件到一个数组中,以便拆分以供以后处理?
我已经设法得到一些工作,将:
读取文件忽略头文件,只将值读入数组。
在数组的每一行上放置一定数量的值。(所以我
可以稍后将其分割成一个放入2d数组)
这是用下面的代码完成的,但是在数组中输入几行后程序就崩溃了。这可能与文件大小有关。
// 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片段是:
[
{ "millis": "1000",
"stamp": "1273010254",
"datetime": "2010/5/4 21:57:34",
"light": "333",
"temp": "78.32",
"vcc": "3.54" },
]
我需要这个JSON的值。例如,我需要“3.54”,但我不希望它打印“vcc”。
如何读取JSON文件,只提取需要放入数组的数据?
Doing this yourself is an awful idea. Use Json.NET. It has already solved the problem better than most programmers could if they were given months on end to work on it. As for your specific needs, parsing into arrays and such, check the documentation, particularly on JsonTextReader. Basically, Json.NET handles JSON arrays natively and will parse them into strings, ints, or whatever the type happens to be without prompting from you. Here is a direct link to the basic code usages for both the reader and the writer, so you can have that open in a spare window while you're learning to work with this.
这是最好的:这次偷懒,使用一个库,这样你就永远解决了这个常见的问题。
对于任何JSON解析,使用网站http://json2csharp.com/(最简单的方法)将您的JSON转换为c#类,以反序列化您的JSON为c#对象。
public class JSONClass
{
public string name { get; set; }
public string url { get; set; }
public bool visibility { get; set; }
public string idField { get; set; }
public bool defaultEvents { get; set; }
public string type { get; set; }
}
然后使用JavaScriptSerializer(来自System.Web.Script.Serialization),以防你不需要任何像newtonsoft这样的第三方DLL。
using (StreamReader r = new StreamReader("jsonfile.json"))
{
string json = r.ReadToEnd();
JavaScriptSerializer jss = new JavaScriptSerializer();
var Items = jss.Deserialize<JSONClass>(json);
}
然后你可以用Items.name或Items获取你的对象。Url等等。
使用开源库Cinchoo ETL,解析非常大的JSON文件是迭代的,使用简单
1. 动态方法:—不需要POCO类
string json = @"
[
{
""millis"": ""1000"",
""stamp"": ""1273010254"",
""datetime"": ""2010/5/4 21:57:34"",
""light"": ""333"",
""temp"": ""78.32"",
""vcc"": ""3.54""
},
{
""millis"": ""2000"",
""stamp"": ""1273010254"",
""datetime"": ""2010/5/4 21:57:34"",
""light"": ""333"",
""temp"": ""78.32"",
""vcc"": ""3.54""
}
]
";
using (var r = ChoJSONReader.LoadText(json))
{
foreach (var rec in r)
Console.WriteLine(rec.Dump());
}
样本提琴:https://dotnetfiddle.net/mo1qvw
2. POCO:受压迫的
定义匹配json属性的POCO类
public class Item
{
public int Millis { get; set; }
public string Stamp { get; set; }
public DateTime Datetime { get; set; }
public string Light { get; set; }
public float Temp { get; set; }
public float Vcc { get; set; }
}
然后使用解析器加载JSON,如下所示
string json = @"
[
{
""millis"": ""1000"",
""stamp"": ""1273010254"",
""datetime"": ""2010/5/4 21:57:34"",
""light"": ""333"",
""temp"": ""78.32"",
""vcc"": ""3.54""
},
{
""millis"": ""2000"",
""stamp"": ""1273010254"",
""datetime"": ""2010/5/4 21:57:34"",
""light"": ""333"",
""temp"": ""78.32"",
""vcc"": ""3.54""
}
]
";
using (var r = ChoJSONReader<Item>.LoadText(json))
{
foreach (var rec in r)
Console.WriteLine(ChoUtility.Dump(rec));
}
样本提琴:https://dotnetfiddle.net/fRWu0w
免责声明:我是这个库的作者。
非常简单的方法,我发现在网上工作。json文件在c#(或任何其他编程语言)
先决条件:-
安装Newtonsoft。Json库到你的项目
Newtonsoft。Json
网址是> https://app.quicktype.io/
步骤
1>去这个URL - https://app.quicktype.io/
复制并粘贴您的JSON文件结构到左侧侧边栏
app.quicktype.io
3>在选项菜单中选择所需语言(这里是c#)
复制生成的代码并转到您的项目并创建一个同名的新.cs文件(这里是“Welcome.cs”)
Welcome.cs
将所有生成的代码粘贴到新创建的类中。
欢迎。cs粘贴代码
6>,就是这样。:)
获取价值的步骤
进入主程序。cs文件或任何你需要访问它的地方。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Access Json values using Keys.>");
String jsonString = new StreamReader("give <.json> file Path here").ReadToEnd();
// use below syntax to access JSON file
var jsonFile = Welcome.FromJson(jsonString);
string FileName = jsonFile.File;
long Lvl = jsonFile.Level;
bool isTrue = jsonFile.CSharp;
Console.WriteLine(FileName);//JSON
Console.WriteLine(Lvl);//1
Console.WriteLine(isTrue);//true
}
}