Iäam 是一个拥有 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 对应的类层次结构:
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