使用vb.net和json.net将嵌套json转换为字符串

使用vb.net和json.net将嵌套json转换为字符串,json,vb.net,facebook,json.net,Json,Vb.net,Facebook,Json.net,我是一个使用json的新手,试图用coal将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/

我是一个使用json的新手,试图用coal将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中获取这些嵌套字段?

如果您已经获得了名称和电子邮件,那么您不会离得太远。我会这样做:

首先,创建与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