【问题标题】:convertin nested json to string with vb.net and json.net使用 vb.net 和 json.net 将嵌套的 json 转换为字符串
【发布时间】:2013-10-09 11:56:37
【问题描述】:

我是一个 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"
}

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

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

我已经尝试了 2 天来寻找简单的解决方案。我很确定有人在我之前使用 vb.net (v4.0) 完成了这个 facebook 抓取?

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

【问题讨论】:

    标签: json vb.net facebook json.net


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多