我正在使用在线API收集电影信息。信息以JSON格式下载。我知道这必须是“反序列化的”,以便能够以VB.NET理解的格式读取它。我已设法这样做,如下所示:
Imports System.Web.Script.Serialization
Public Class Form1
Dim serializer As JavaScriptSerializer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
serializer = New JavaScriptSerializer()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim json
Dim cast1
Dim objXMLHTTP
Dim urltxt
urltxt = "http://api.themoviedb.org/3/movie/" + IDText.Text + "/casts?api_key=***"
objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open("GET", urltxt, False)
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
json = System.Text.Encoding.ASCII.GetString(objXMLHTTP.ResponseBody)
Else
json = ""
End If
Dim result As Object = serializer.Deserialize(Of Object)(json)
cast1 = result.Item("cast")(0)("name")
Label1.Text = cast1
End Sub
End Class
因此,当表单加载时,会启动JavaScriptSerializer。然后,在表单上,用户输入id no。一部电影,然后点击Button1
。在此事件中,变量urltxt
定义为输入movie id
。然后我不确定objXMLHTTP
部分是如何工作的。我知道检索的数据由一个字节流组成,因此ResponseBody
必须转换为ASCII。对于ID值为550(搏斗俱乐部的ID),我得到了这个JSON响应(注意:大多数情况下这个数据是一致的,所以不要过于烦恼):
200 (OK)
Content-Type: application/json
{
"id": 550,
"cast": [
{
"id": 819,
"name": "Edward Norton",
"character": "The Narrator",
"order": 0,
"profile_path": "/7cf2mCVI0qv2PnZVNbbEktS8Xae.jpg"
},
{
"id": 287,
"name": "Brad Pitt",
"character": "Tyler Durden",
"order": 1,
"profile_path": "/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg"
},
{
"id": 1283,
"name": "Helena Bonham Carter",
"character": "Marla Singer",
"order": 2,
"profile_path": "/s5WtwCxOuPR3aIYYZ06Cof7tw2A.jpg"
},
{
"id": 7470,
"name": "Meat Loaf",
"character": "Robert 'Bob' Paulson",
"order": 3,
"profile_path": "/AsYkaWwsNCxORU372t0isSDDyad.jpg"
},
{
"id": 7471,
"name": "Zach Grenier",
"character": "Richard Chesler",
"order": 4,
"profile_path": "/vtKQjYDLvx8AhUZtyYS61pU7DRF.jpg"
},
{
"id": 7472,
"name": "Richmond Arquette",
"character": "Intern",
"order": 5,
"profile_path": null
},
{
"id": 7219,
"name": "David Andrews",
"character": "Thomas",
"order": 6,
"profile_path": "/957tUR5s9FV3J25ruGmjQD8rD7A.jpg"
},
{
"id": 7473,
"name": "Rachel Singer",
"character": "Chloe",
"order": 7,
"profile_path": "/aYlqeJIrFmbRAScxqhBSPVrG2pX.jpg"
},
{
"id": 7497,
"name": "Holt McCallany",
"character": "The Mechanic",
"order": 8,
"profile_path": "/1xR6O1dkKDJ9ypurlKfQ1qqHv4n.jpg"
},
{
"id": 7498,
"name": "Eion Bailey",
"character": "Ricky",
"order": 9,
"profile_path": "/aqDGV2NWABCnENOlPrd2lchs45n.jpg"
},
{
"id": 7499,
"name": "Jared Leto",
"character": "Angel Face",
"order": 10,
"profile_path": "/9jZCWhSx8NU3VHB6TRzUvgWQK3I.jpg"
},
{
"id": 7500,
"name": "Peter Iacangelo",
"character": "Lou",
"order": 11,
"profile_path": null
}
],
"crew": [
{
"id": 7469,
"name": "Jim Uhls",
"department": "Writing",
"job": "Author",
"profile_path": null
},
{
"id": 7474,
"name": "Ross Grayson Bell",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 7475,
"name": "Ceán Chaffin",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 1254,
"name": "Art Linson",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 7476,
"name": "David Prior",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 7477,
"name": "John King",
"department": "Sound",
"job": "Original Music Composer",
"profile_path": null
},
{
"id": 7478,
"name": "Michael Simpson",
"department": "Sound",
"job": "Original Music Composer",
"profile_path": null
},
{
"id": 7479,
"name": "Jeff Cronenweth",
"department": "Camera",
"job": "Director of Photography",
"profile_path": null
},
{
"id": 7480,
"name": "James Haygood",
"department": "Editing",
"job": "Editor",
"profile_path": null
},
{
"id": 7481,
"name": "Laray Mayfield",
"department": "Production",
"job": "Casting",
"profile_path": null
},
{
"id": 1303,
"name": "Alex McDowell",
"department": "Art",
"job": "Production Design",
"profile_path": null
},
{
"id": 7763,
"name": "Ren Klyce",
"department": "Sound",
"job": "Sound Editor",
"profile_path": null
},
{
"id": 7764,
"name": "Richard Hymns",
"department": "Sound",
"job": "Sound Editor",
"profile_path": null
},
{
"id": 7467,
"name": "David Fincher",
"department": "Directing",
"job": "Director",
"profile_path": "/iTWJyV1yUoagqDzxyTjxoPuSVs.jpg"
},
{
"id": 7468,
"name": "Chuck Palahniuk",
"department": "Writing",
"job": "Novel",
"profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
}
]
}
此后,变量json的内容被反序列化。这是我最想了解的部分。究竟是什么类型的对象正在创建? Item属性如何工作?我想理解这一点,所以我可以计算JSON数据的'cast'元素中的元素数量。我知道代码有效,因为使用result.Item("cast")(0)("name")
函数会返回“Edward Norton”,这是正确的。
答案 0 :(得分:0)
deserilzed对象是具有正确属性的类型。您可以使用以下链接http://json2csharp.com/从有效的json生成类/类。 你的例子是:
public class Cast
{
public int id { get; set; }
public string name { get; set; }
public string character { get; set; }
public int order { get; set; }
public string profile_path { get; set; }
}
public class Crew
{
public int id { get; set; }
public string name { get; set; }
public string department { get; set; }
public string job { get; set; }
public string profile_path { get; set; }
}
public class RootObject
{
public int id { get; set; }
public List<Cast> cast { get; set; }
public List<Crew> crew { get; set; }
}
基本上,您可以使用正确的属性创建类,如果映射正确,则反序列化器会创建具体类型的实例。 Json和您定义的类应匹配。