0

我是一个带有 json 的新手,并试图用煤将 facebook json 读取到 vb.net 以将其保存到数据库中。

{
    "id": "1154546722",
    "name": "Toni Laket",
    "first_name": "Toni",
    "last_name": "Laket",
    "link": "https://www.facebook.com/tbll",
    "username": "arbous",
    "birthday": "07/11/1969",
    "hometown": {
        "id": "1031215454756",
        "name": "Harmo, Land"
    },
    "location": {
        "id": "1031215454756",
        "name": "Harmo, Land"
    },
    "work": [
        {
            "employer": {
                "id": "5440547873",
                "name": "Sytyty Oy"
            },
            "location": {
                "id": "107234324406",
                "name": "Pori"
            },
            "position": {
                "id": "14625323232414",
                "name": "Keaxrrjohtaja"
            },
            "start_date": "1999-01-01"
        }
    ],
    "education": [
        {
            "school": {
                "id": "106444432115435",
                "name": "ukio"
            },
            "type": "High School"
        }
    ],
    "gender": "male",
    "email": "tddd@arpo.com",
    "timezone": 3,
    "locale": "fi_FI",
    "verified": true,
    "updated_time": "2013-10-09T05:32:47+0000"
}

所以我试图将所有这些信息获取到数据库表中。我已经设法保存了电子邮件、姓名等基本信息。

地点、家乡和工作呢?我如何获取这些信息?

两天来,我一直在努力寻找简单的解决方案。我很确定有人在我之前用 vb.net (v4.0) 完成了这个 facebook 抓取?

我一直在使用 json.net,但没有设法获得那些嵌套的 json 字段。如何使用 json.net 和 vb.net 获取那些嵌套字段?

4

1 回答 1

0

如果您已经获得了姓名和电子邮件,那么您就离得不远了。这是我的做法:

首先,创建与您的 JSON 对应的类层次结构:

Public Class Person
    Public Property id As String
    Public Property name As String
    Public Property first_name As String
    Public Property last_name As String
    Public Property link As String
    Public Property username As String
    Public Property birthday As String
    Public Property hometown As Hometown
    Public Property location As Location
    Public Property work As List(Of Work)
    Public Property education As List(Of Education)
    Public Property gender As String
    Public Property email As String
    Public Property timezone As Integer
    Public Property locale As String
    Public Property verified As Boolean
    Public Property updated_time As String
End Class

Public Class Hometown
    Public Property id As String
    Public Property name As String
End Class

Public Class Location
    Public Property id As String
    Public Property name As String
End Class

Public Class Work
    Public Property employer As Employer
    Public Property location As Location
    Public Property position As Position
    Public Property start_date As String
End Class

Public Class Employer
    Public Property id As String
    Public Property name As String
End Class

Public Class Position
    Public Property id As String
    Public Property name As String
End Class

Public Class Education
    Public Property school As School
    Public Property type As String
End Class

Public Class School
    Public Property id As String
    Public Property name As String
End Class

接下来,将 JSON 反序列化到您的类中,如下所示:

Dim person As Person = JsonConvert.DeserializeObject(Of Person)(json)

最后,使用您认为合适的数据。

Console.WriteLine("name: " + person.name)
Console.WriteLine("email: " + person.email)
Console.WriteLine("gender: " + person.gender)
Console.WriteLine("birthday: " + person.birthday)
Console.WriteLine("hometown: " + person.hometown.name)
Console.WriteLine("work:")
For Each work As Work In person.work
    Console.WriteLine(vbTab + "employer: " + work.employer.name)
    Console.WriteLine(vbTab + "position: " + work.position.name)
    Console.WriteLine(vbTab + "location: " + work.location.name)
    Console.WriteLine(vbTab + "start date: " + work.start_date)
Next
Console.WriteLine("education:")
For Each education As Education In person.education
    Console.WriteLine(vbTab + "school: " + education.school.name)
    Console.WriteLine(vbTab + "type: " + education.type)
Next

示例输出:

name: Toni Laket
email: tddd@example.org
gender: male
birthday: 07/11/1969
hometown: Harmo, Land
work:
        employer: Sytyty Oy
        position: Keaxrrjohtaja
        location: Pori
        start date: 1999-01-01
education:
        school: ukio
        type: High School
于 2013-10-10T05:04:30.520 回答