3

I'm trying to parse this array using JSON.NET

[[{"event_timestamp":1304587800,"event_time_8601":"2011-05-05T19:00:00+09:30","event_date":"Thursday, May 5th, 2011","event_time":"7:00 pm","venue_phone":"","event_notes":"","event_id":"2690119","links":[{"link_url":"http:\/\/tickets.sonicliving.com\/event\/2690119\/sl-tickets","link_title":"7:00 pm","link_type":"ticket"}],"event_title":"Justin Bieber","rsvp":"3","venue_id":"83659","venue_name":"Entertainment Centre Adelaide","venue_address":"","lat":"0","lon":"0","poster_url_large":"http:\/\/posters.sonicliving.com\/event\/2690119\/poster.png","poster_url_small":"http:\/\/posters.sonicliving.com\/event\/2690119\/smallposter.png","venue_city":"Adelaide","venue_state":"05","venue_country":"AU","eid":"181570318520944","eids":[0],"facebook_event_url":"http:\/\/www.facebook.com\/event.php?eid=181570318520944"}],[{"event_timestamp":1304766000,"event_time_8601":"2011-05-07T19:00:00+08:00","event_date":"Saturday, May 7th, 2011","event_time":"7:00 pm","venue_phone":"","event_notes":"","event_id":"2690126","links":[{"link_url":"http:\/\/tickets.sonicliving.com\/event\/2690126\/sl-tickets","link_title":"7:00 pm","link_type":"ticket"}],"event_title":"Justin Bieber","rsvp":"0","venue_id":"76921","venue_name":"Burswood Theatre","venue_address":"Great Eastern Highway","lat":"0","lon":"0","poster_url_large":"http:\/\/posters.sonicliving.com\/event\/2690126\/poster.png","poster_url_small":"http:\/\/posters.sonicliving.com\/event\/2690126\/smallposter.png","venue_city":"Perth","venue_state":"08","venue_country":"AU","eid":"173126412708252","eids":[0],"facebook_event_url":"http:\/\/www.facebook.com\/event.php?eid=173126412708252"}]]

I've tried the following code, but all I get is an exception:

Dim jResults As JArray = JArray.Parse(strJResults)

MessageBox.Show("You have " & jResults.Count & " results")

For Each jTok As JToken In jResults
    txtResult.Text = txtResult.Text & vbCrLf & jTok.Item("event_date").ToString()
Next
System.ArgumentException was unhandled
  Message="Accessed JArray values with invalid key value: "event_date". Array position index expected."
  Source="Newtonsoft.Json.Net35"
  StackTrace:
       at Newtonsoft.Json.Linq.JArray.get_Item(Object key) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Linq\JArray.cs:line 187
       at JSONNetTest.frmMain.frmMain_Load(Object sender, EventArgs e) in C:\Users\John Meyer\Documents\Visual Studio 2008\Projects\JSONNetTest\JSONNetTest\frmMain.vb:line 25
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException:

Any ideas?

2 Answers 2

2

The message "Array position index expected" is the clue to solving this. (It says: "Hey, I'm an array! Stop trying to ask me for a named property!")

Look at the format again:

[ [{...}], [{...}] ]

Each object with "event_date" is nested in its own array (of one element) which is in turn an element of the outer array. This secondary array also needs to be descended into before the "event_date" property can be accessed.

Consider the access should then be:

jTok.Item(0).Item("event_date")

Happy coding.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It's not one of those easy training arrays that I've been looking at.
-1
Sub Button5Click(sender As Object, e As EventArgs)
Dim APP As New Excel.Application
    Dim worksheet As Excel.Worksheet
    Dim worksheet2 As Excel.Worksheet
    Dim LastRow As Integer
            Dim workbook As Excel.Workbook

            Dim sourceString As String
            Dim url As String

            Dim i As Integer

            Dim wc As New System.Net.WebClient

            workbook = App.Workbooks.Open(room.Text)
            worksheet = workbook.Worksheets("sheet1")
            worksheet2 = workbook.Worksheets("sheet2")
            LastRow = worksheet.UsedRange.Rows.Count           

            For rrow As Integer =2 To LastRow
                url = CStr(worksheet.Cells(rrow, 1).Value)
                ' MsgBox(url)
            Try         

                wc.Headers("User-Agent")="Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"  ' In my case user agent was table.
                sourceString = wc.DownloadString(url) 
                sourceString=sourceString.Trim()
                'MsgBox(sourceString)
                row.Text="URL# " & rrow-1 & " is getting processed"
                urlno.Text=url

                Dim allData As JArray = JArray.Parse(sourceString)
                i=0
                Try
                    For i=0 To allData.Count-1

                    Dim token As JToken=allData.First


                    Do Until token Is Nothing   

                        msgbox(token.Item("text").ToString())



                    msgbox(allData.Count & i+1) 
                    token=token.Next
                    Loop
                    Next


                Catch ex As Exception
                    worksheet.Cells(rrow, 2).Value=ex.ToString()
                    MsgBox(ex.ToString())

                End Try 

            Catch Outerex As Exception
                worksheet.Cells(rrow,2 ).Value="Unexpected Error"   
                MsgBox(Outerex.ToString())
            End Try

            Next
            MsgBox ("Done")     
            workbook.Save()
            workbook.Close()       
            App.Quit()      
    End Sub

1 Comment

Care to add some comments, about what you are doing?

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.