为Visual Studio 2010项目
问题描述:
我有一个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
感谢
答
工作的呢?
Dim collection As JObject = DirectCast(jsonResults("belongs_to_collection"), JObject)
然后从集合JObject中获取其余部分,就像您对顶级JObject所做的一样?
我试过,但之前再次尝试只是为了确保...这是行不通的。它会返回错误:无法投型“Newtonsoft.Json.Linq.JValue”的对象键入“Newtonsoft.Json.Linq.JObject” – jaminben
我还应该说,“belongs_to_collection”并不总是包含值。正因为如此,我试着测量结果为null,“”,DbNull.value等,但也没有成功。 – jaminben
好了,所以它的现在进行排序......肯定我尝试过这一点,它没有工作,但它似乎是现在的工作。解决方案在主帖中。 – jaminben