2014-04-09 70 views
0

我的輸入是這樣的:JSON.Net - 讀/ JSON解析行的文件

 
[ 
{ 
"somenums": "1", 
"someDate": "1.1.2014", 
"viewdata": "1119958", 
"visitdata": "152452", 
"uniquedata": "125873" 
}, 
{ 
"somenums": "2", 
"someDate": "2.1.2014", 
"viewdata": "1863752", 
"visitdata": "241453", 
"uniquedata": "200762" 
} 
] 

我一直沒能找到如何處理一個文件中像這樣含有多行很多。我發現的一小部分信息是Json會將包含在方括號中的對象反序列化爲List。

摸索周圍擺脫我已經結束了與下面的代碼的語法錯誤,但得到的聲明jrrows = DirectCast(JsonConvert.

If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then 

     My.Settings.LastJsonFileDirectory = Path.GetDirectoryName(ofd.FileName) 
     My.Settings.Save() 

     Dim sr As New StreamReader(ofd.FileName) 
     Dim jrrows As List(Of JsonRow) 
     jrrows = DirectCast(JsonConvert.DeserializeObject(sr.ReadToEnd), List(Of JsonRow)) 


    End If 
End Sub 
End Class 

<Serializable()> _ 
Public Class JsonRow 

    Public somenums As String 
    Public someDate As String 
    Public viewdata As String 
    Public visitdata As String 
    Public uniquedata As String 

    Sub New() 'we need a parameter-less constructor to make it serializable 
    End Sub 
End Class 
+0

這不是「行」。它只是一個JSON對象數組。 – mason

回答

0

在C#中運行時InvalidDirectCast例外......之後

string json=File.ReadAllText(ofd.FileName); 
List<JsonRow> mydata = JsonConvert.Deserialize<List<JsonRow>>(json); 

在VB.NET中(自動翻譯)

Dim json As String = File.ReadAllText(ofd.FileName) 
Dim mydata As List(Of JsonRow) = JsonConvert.Deserialize(Of List(Of JsonRow))(json)