我花了兩天的大部分時(shí)間來“打理”代碼示例等,試圖將一個(gè)非常大的JSON文件讀入c#中的數(shù)組,以便稍后將其拆分為2d數(shù)組進(jìn)行處理。我遇到的問題是我找不到任何人在做我想做的事的例子。這意味著我只是在編輯代碼,以期獲得最佳效果。我設(shè)法使某些東西能夠工作:讀取文件Missing out標(biāo)頭,僅將值讀入數(shù)組。在數(shù)組的每一行上放置一定數(shù)量的值。(所以我以后可以將它分割成2d數(shù)組)這是用下面的代碼完成的,但是在數(shù)組中輸入幾行后,它使程序崩潰。這可能與文件大小有關(guān)。// 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 statementif (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文件,并且僅提取所需的數(shù)據(jù)并將其放入數(shù)組中,或以后可用于放入數(shù)組中的內(nèi)容。
3 回答

大話西游666
TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
根據(jù)@LB的解決方案,(鍵入為Object而不是Anonymous)VB代碼為
Dim oJson as Object = JsonConvert.DeserializeObject(File.ReadAllText(MyFilePath))
我應(yīng)該提到,這對(duì)于構(gòu)造不需要類型的HTTP調(diào)用內(nèi)容是快速且有用的。使用Object而不是Anonymous意味著您可以O(shè)ption Strict On在Visual Studio環(huán)境中進(jìn)行維護(hù)-我討厭將其關(guān)閉。
- 3 回答
- 0 關(guān)注
- 2244 瀏覽
添加回答
舉報(bào)
0/150
提交
取消