gpt4 book ai didi

vb.net - 解析 JSON 数组

转载 作者:行者123 更新时间:2023-12-03 00:40:53 26 4
gpt4 key购买 nike

我正在尝试使用 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"}]]

我尝试了以下代码,但我得到的只是一个异常:

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:

有什么想法吗?

最佳答案

消息“预期数组位置索引”是解决此问题的线索。 (它说:“嘿,我是一个数组!别再向我索要命名属性了!”)

再看一下格式:

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

每个带有“event_date”的对象都嵌套在它自己的数组中(一个元素),而该数组又是外部数组的一个元素。在访问“event_date”属性之前,还需要降级到该辅助数组。

考虑访问应该是:

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

祝你编码愉快。

关于vb.net - 解析 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5905649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com