我有一个来自 HTTP 请求的 JSON 响应,但我无法拼凑出拼图的最后一部分……我已经完成了 90% 的工作,但现在我被卡住了,找不到任何线索别处。
以下是我得到的回复:
{
"adult": false,
"backdrop_path": "/mOTtuakUTb1qY6jG6lzMfjdhLwc.jpg",
"belongs_to_collection": {
"backdrop_path": "/mOTtuakUTb1qY6jG6lzMfjdhLwc.jpg",
"id": 10,
"name": "Star Wars Collection",
"poster_path": "/6rddZZpxMQkGlpQYVVxb2LdQRI3.jpg"
},
"budget": 11000000,
"genres": [
{
"id": 28,
"name": "Action"
},
{
"id": 14,
"name": "Fantasy"
},
{
"id": 878,
"name": "Science Fiction"
}
],
"homepage": "http://www.starwars.com",
"id": 11,
"imdb_id": "tt0076759",
"original_title": "Star Wars: Episode IV: A New Hope",
"overview": "Princess Leia is captured.",
"popularity": 84.8,
"poster_path": "/qoETrQ73Jbd2LDN8EUfNgUerhzG.jpg",
"production_companies": [
{
"id": 1,
"name": "Lucasfilm"
},
{
"id": 8265,
"name": "Paramount"
}
],
"production_countries": [
{
"iso_3166_1": "TN",
"name": "Tunisia"
},
{
"iso_3166_1": "US",
"name": "United States of America"
}
],
"release_date": "1977-12-27",
"revenue": 775398007,
"runtime": 121,
"spoken_languages": [
{
"iso_639_1": "en",
"name": "English"
}
],
"tagline": "A long time ago in a galaxy far, far away...",
"title": "Star Wars: Episode IV: A New Hope",
"vote_average": 8.8,
"vote_count": 75
}
使用 Newtonsoft Json,除了“belongs_to_collection”部分之外,我可以获得所需的一切......到目前为止,我的代码如下:
Dim jsonResults As JObject = JObject.Parse(searchResults)
Dim genreItems As JArray = DirectCast(jsonResults("genres"), JArray)
Dim productionCompaniesItems As JArray = DirectCast(jsonResults("production_companies"), JArray)
Dim release_Date As String = CStr(jsonResults.SelectToken("release_date"))
Dim overview As String = CStr(jsonResults.SelectToken("overview"))
Dim homepage As String = CStr(jsonResults.SelectToken("homepage"))
Dim tagline As String = CStr(jsonResults.SelectToken("tagline"))
Dim imdb_id As String = CStr(jsonResults.SelectToken("imdb_id"))
Dim vote_Average As String = CStr(jsonResults.SelectToken("vote_average").ToString)
Dim popularity As String = CStr(jsonResults.SelectToken("popularity").ToString)
Dim vote_Count As String = CStr(jsonResults.SelectToken("vote_count").ToString)
Dim revenue As String = CStr(jsonResults.SelectToken("revenue").ToString)
Dim runtime As String = CStr(jsonResults.SelectToken("runtime").ToString)
Dim budget As String = CStr(jsonResults.SelectToken("budget").ToString)
Dim adult As Boolean = CBool(jsonResults.SelectToken("adult"))
Dim item As JObject
Dim jtoken As JToken
'Genre List
For i As Integer = 0 To genreItems.Count - 1
item = DirectCast(genreItems(i), JObject)
jtoken = item.First
While jtoken IsNot Nothing
Dim jProperty = DirectCast(jtoken, JProperty).Name.ToString()
If jProperty = "name" Then
'Debug.Print("Genres: " & DirectCast(jtoken, JProperty).Value.ToString())
End If
jtoken = jtoken.[Next]
End While
Next
谁能指出我正确的方向,这样我就可以完成最后的工作?
谢谢
编辑
Dim collection As JObject = DirectCast(jsonResults("belongs_to_collection"), JObject)
Dim id As String = CStr(collection.SelectToken("id").ToString)
当“belongs_to_collection”包含数据时,这确实有效,但当它没有错误时它会出错:
Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject'
在尝试从中获取任何内容之前,如何测试它是否包含数据?
编辑 2 好的,所以它现在排序了......我很确定我之前尝试过它并没有工作,但它现在似乎工作了。解决方案是:
Dim test_Collection As String = CStr(jsonResults.SelectToken("belongs_to_collection").ToString)
If test_Collection = "" Then
Console.WriteLine("--- NOTHING ---")
Else
Dim collection As JObject = DirectCast(jsonResults("belongs_to_collection"), JObject)
Dim id As String = CStr(collection.SelectToken("id").ToString)
Console.WriteLine(id)
End If
谢谢