0

My input looks like this:

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

I haven't been able to find a lot on how to handle a file like this containing multiple rows. One scrap of info I did find is that Json will deserialize objects enclosed in square brackets as a List.

After fumbling around to get rid of syntax errors I have ended up with the code below but get a runtime InvalidDirectCast exception on the statement jrrows = DirectCast(JsonConvert.etc.

    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
1
  • It's not "rows". It's just an array of JSON objects. Commented Apr 11, 2014 at 20:03

1 Answer 1

1

In C#...

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

In VB.NET (auto translated)

Dim json As String = File.ReadAllText(ofd.FileName)
Dim mydata As List(Of JsonRow) = JsonConvert.Deserialize(Of List(Of JsonRow))(json)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.