gpt4 book ai didi

json - 如何从 JSON 中提取特定对象?

转载 作者:行者123 更新时间:2023-12-04 00:13:49 40 4
gpt4 key购买 nike

我想知道如何从 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:
(http://www.stginternational.org/wp-json/wp/v2/users)
是一个对象数组。
可以使用 JArray.Parse() 对其进行解析,但我建议将此 JSON 反序列化为 .Net 类:它更容易处理。

JSON 的基础对象(数组中的每个对象)定义如下:

{
"id": 1,
"name": "drall",
"url": "",
"description": "",
"link": "http://www.stginternational.org/author/drall/",
"slug": "drall",
"avatar_urls": {
"24": "http://1.gravatar.com/avatar/dc6dd0ef71784957b629e124f19364cb?s=24&d=mm&r=g",
"48": "http://1.gravatar.com/avatar/dc6dd0ef71784957b629e124f19364cb?s=48&d=mm&r=g",
"96": "http://1.gravatar.com/avatar/dc6dd0ef71784957b629e124f19364cb?s=96&d=mm&r=g"
},
"meta": [],
"_links": {
"self": [
{
"href": "http://www.stginternational.org/wp-json/wp/v2/users/1"
}
],
"collection": [
{
"href": "http://www.stginternational.org/wp-json/wp/v2/users"
}
]
}
}

它可以用这些 .Net 类来表示:

Public Class UserObject
Public Property Id As Long
Public Property Name As String
Public Property Url As String
Public Property Description As String
Public Property Link As Uri
Public Property Slug As String
<JsonProperty("avatar_urls")>
Public Property AvatarUrls As Dictionary(Of String, Uri)
Public Property Meta As List(Of Object)
<JsonProperty("_links")>
Public Property Links As Links
End Class

Public Class Links
Public Property Self As List(Of LinkCollection)
Public Property Collection As List(Of LinkCollection)
End Class

Public Class LinkCollection
Public property Href As Uri
End Class

有了这个模型,你可以简单地使用 JsonConvert.DeserializeObject() ,指定要反序列化到的类型。
如前所述,这是一个对象数组或列表,其中基础对象是 UserObject,因此您可以指定 List(Of UserObject) :

Dim json = JsonConvert.DeserializeObject(Of List(Of UserObject))(json)

然后您可以照常访问类对象:

Imports System.Net
Imports Newtonsoft.Json

Dim users As List(Of UserObject) = Nothing

Using client As New WebClient()
Dim json = client.DownloadString([The URL])
users = JsonConvert.DeserializeObject(Of List(Of UserObject))(json)
End Using

If users IsNot Nothing Then
For Each user In users
Console.WriteLine(user.Slug)
Console.WriteLine(user.Links.Self(0).Href)
Console.WriteLine(user.Links.Collection(0).Href)

For Each avatar In user.AvatarUrls
Console.WriteLine($"Key: {avatar.Key}, Value: {avatar.Value}")
Next
Next
End If

如果您只需要其中一个属性(在本例中为 slug),您可以使用 JArray.Parse()解析 JSON 并直接读取属性值:

Using client As New WebClient()
Dim json = client.DownloadString([The URL])
Dim users = JArray.Parse(json)
For Each user As JToken In users
Console.WriteLine(user("slug"))
Next
End Using

关于json - 如何从 JSON 中提取特定对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65595760/

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