【发布时间】:2021-01-06 12:26:57
【问题描述】:
我想知道如何从 JSON 中提取特定对象。
在发布此之前,我在 Stackoverflow 上看到了大部分问题已解决,但没有人已经谈论过这个。
我想从 JSON 对象中获取 slug 值。
这是我的代码Get Users From JSON
Imports System
Imports Newtonsoft.Json.Linq
Public Module Module1
Public Sub Main()
Dim myJsonString = New System.IO.StreamReader(New System.Net.WebClient().
OpenRead("https://pastebin.com/raw/z4GZFuF3")).ReadToEnd()
Dim myJObject = JObject.Parse(myJsonString)
For Each match In myJObject("matches")
Console.WriteLine(match("id")("slug"))
Next
End Sub
End Module
这是输出:
Run-time exception (line -1): Error reading JObject from JsonReader.
Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Stack Trace:
[Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader.
Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.]
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)
at Newtonsoft.Json.Linq.JObject.Parse(String json)
at Module1.Main()
由于此错误,我减少的是 JSON 文本中不存在对象 "matches",但我不知道我应该在其位置指定什么才能使其正常工作。
【问题讨论】:
-
该链接中 JSON 的根是 JArray,而不是 JObject,并且没有
matches对象。我建议将此 JSON 作为 .Net 类进行解析和处理。 -
你好 Jimi,谢谢你的回复,你能给我一个例子吗?因为你已经知道我不太了解开发我是初学者,提前谢谢你。
-
在你向我解释问题后我找到了这个例子,这对我有帮助吗? How To Parse JSON In .NET Core