0

我正在使用在线 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 号。电影,然后单击Button1。在此事件中,变量urltxt是使用movie id输入定义的。然后我不确定这objXMLHTTP部分是如何工作的。我知道检索到的数据是由字节流组成的,因此ResponseBody必须将其转换为 ASCII。对于 550 的 id 值,即 Fight Club 的 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”,这是正确的。

4

1 回答 1

0

deserilized 对象是具有正确属性的类型。例如,您可以使用以下链接从有效的 json 生成一个/类:http: //json2csharp.com/。对于您的示例将是:

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 和您定义的类应该匹配。

于 2013-09-08T13:37:50.590 回答