我有一個HTTP請求的JSON響應,我無法放在一起拼圖的最後peice的JSON迴應...我已經訂出90%的什麼,我需要做的,但現在我被卡住了,在別處找不到任何線索。爲Visual Studio 2010項目
下面是我得到的迴應:
{
"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
任何人都可以點我在正確的方向,所以我可以得到最後peice的完成了嗎?
感謝
編輯
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
感謝
我試過,但之前再次嘗試只是爲了確保...這是行不通的。它會返回錯誤:無法投型「Newtonsoft.Json.Linq.JValue」的對象鍵入「Newtonsoft.Json.Linq.JObject」 – jaminben
我還應該說,「belongs_to_collection」並不總是包含值。正因爲如此,我試着測量結果爲null,「」,DbNull.value等,但也沒有成功。 – jaminben
好了,所以它的現在進行排序......肯定我嘗試過這一點,它沒有工作,但它似乎是現在的工作。解決方案在主帖中。 – jaminben