我从JSON流中提取了一些数据,其中一个元素是一个数组。结果是
"{" & vbCrLf & " ""2019-08-22"": 128.75," & vbCrLf & " ""2019-08-23"": 151.47," & vbCrLf & " ""2019-08-24"": 151.47" & vbCrLf & "}"
然后我尝试将这三个值分开,并使用VB将它们放入数据库中。
尝试解析,反序列化但绕圈转。
Dim uObject = Newtonsoft.Json.Linq.JObject.Parse("{" & vbCrLf & " ""2019-08-22"": 128.75," & vbCrLf & " ""2019-08-23"": 151.47," & vbCrLf & " ""2019-08-24"": 151.47" & vbCrLf & "}")
DEBUG.PRINT(uObject(1, 1)) - This fails
试图获得以下输出
Key1: 2019-08-22 Value1: 128.75
Key2: 2019-08-23 Value1: 151.47
Key3: 2019-08-24 Value1: 151.47
答案 0 :(得分:0)
我做了一个快速测试:
Dim JSON_Obj As JObject = JObject.Parse("{" & vbCrLf & " ""2019-08-22"": 128.75," & vbCrLf & " ""2019-08-23"": 151.47," & vbCrLf & " ""2019-08-24"": 151.47" & vbCrLf & "}")
For Each item In JSON_Obj
Debug.Print(String.Format("Key: {0} Value: {1}", item.key, item.value))
Next
'>Key: 2019-08-22 Value: 128.75
'>Key: 2019-08-23 Value: 151.47
'>Key: 2019-08-24 Value: 151.47
如果不能通过键名获取它,只需使用数组索引或类似的东西获取它们,例如JSON_Obj1 = JSON_Obj.Children.ToArray()(0)
答案 1 :(得分:0)
在CruelD的大力帮助下;我最后的工作代码是
Dim JSON_Obj As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse("{" & vbCrLf & " ""2019-08-22"": 128.75," & vbCrLf & " ""2019-08-23"": 151.47," & vbCrLf & " ""2019-08-24"": 151.47" & vbCrLf & "}")
Dim JSON_Data As List(Of JToken) = JSON_Obj.Children().ToList
For Each uItem As JProperty In JSON_Data
Debug.Print(uItem.Name.ToString & " - " & uItem.Value.ToString)
Next
这很有效。