ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 529|ตอบกลับ: 0

[VB.NET] การนำข้อมูลรหัสไปรษณีย์จากรูปแบบ JSON มาแสดงผลลงในตารางกริด และค้นหาข้อมูลได้

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6060

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6060

ก็เป็นอีกหนึ่งแหล่งที่มาของไฟล์ JSON ที่มีข้อมูลรหัสไปรษณีย์ทั่วประเทศไทย แต่รูปแบบ JSON ของผู้จัดทำเขาออกแนวกลับด้าน คือใช้รหัสไปรษณีย์ในการค้นหาชื่อตำบล อำเภอและจังหวัด แต่โดยปกติเราจะค้นหาด้วยชื่อตำบล อำเภอและจังหวัด เพื่อหารหัสไปรษณีย์มากกว่า พอนำ JSON ชุดนี้มาใช้งาน ก็เลยต้องเขียนโค้ดขึ้นมายากสักหน่อย ในการแยกเอาข้อมูลตำบล อำเภอ จังหวัดที่มันตรงกันเท่านั้น ด้วยการใช้งาน Lambda Expression ... อย่างไรก็ตามเราก็ใช้หลักการเดิม คือ การ Deserialize JSON แยกเอาข้อมูลมาเก็บไว้ใน DataTable ก่อน จากนั้นก็จะทำ BindingSource อีกที แล้วนำไปแสดงผลลงในตารางกริด ส่วนในการค้นหาก็จะใช้ Method Filter ...

แหล่งข้อมูลต้นฉบับไฟล์ JSON ...





Add Reference ... NewtonSoft

ตัวอย่างรูปแบบ JSON ...
  1. {
  2.     "zipCode": "10100",
  3.     "subDistrictList": [
  4.       {
  5.         "subDistrictId": "100801",
  6.         "districtId": "1008",
  7.         "provinceId": "10",
  8.         "subDistrictName": "ป้อมปราบ"
  9.       },
  10.       {
  11.         "subDistrictId": "100802",
  12.         "districtId": "1008",
  13.         "provinceId": "10",
  14.         "subDistrictName": "วัดเทพศิรินทร์"
  15.       },
  16.       {
  17.         "subDistrictId": "100803",
  18.         "districtId": "1008",
  19.         "provinceId": "10",
  20.         "subDistrictName": "คลองมหานาค"
  21.       },
  22.       {
  23.         "subDistrictId": "100804",
  24.         "districtId": "1008",
  25.         "provinceId": "10",
  26.         "subDistrictName": "บ้านบาตร"
  27.       },
  28.       {
  29.         "subDistrictId": "100805",
  30.         "districtId": "1008",
  31.         "provinceId": "10",
  32.         "subDistrictName": "วัดโสมนัส"
  33.       },
  34.       {
  35.         "subDistrictId": "101301",
  36.         "districtId": "1013",
  37.         "provinceId": "10",
  38.         "subDistrictName": "จักรวรรดิ"
  39.       },
  40.       {
  41.         "subDistrictId": "101302",
  42.         "districtId": "1013",
  43.         "provinceId": "10",
  44.         "subDistrictName": "สัมพันธวงศ์"
  45.       },
  46.       {
  47.         "subDistrictId": "101303",
  48.         "districtId": "1013",
  49.         "provinceId": "10",
  50.         "subDistrictName": "ตลาดน้อย"
  51.       }
  52.     ],
  53.     "districtList": [
  54.       {
  55.         "districtId": "1008",
  56.         "districtName": "ป้อมปราบศัตรูพ่าย",
  57.         "provinceId": "10"
  58.       },
  59.       {
  60.         "districtId": "1013",
  61.         "districtName": "สัมพันธวงศ์",
  62.         "provinceId": "10"
  63.       }
  64.     ],
  65.     "provinceList": [
  66.       {
  67.         "provinceId": "10",
  68.         "provinceName": "กรุงเทพมหานคร"
  69.       }
  70.     ]
  71.   }
คัดลอกไปที่คลิปบอร์ด

มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. '// Special Thank ... Original JSON data.
  2. '// https://gist.github.com/mennwebs/8ff8e27a01fd06ca2ac965a1c7317552

  3. Imports Newtonsoft.Json
  4. Imports System.IO

  5. Public Class frmJsonPostCode
  6.     Private JsonSource As New BindingSource()

  7.     Private Sub frmJsonPostCode_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  8.         '// JSON data as a string
  9.         Dim json = File.ReadAllText(MyPath(Application.StartupPath) & "src\th-address.json")
  10.         '// Deserialize JSON into a List(Of LocationData)
  11.         Dim dataList As List(Of LocationData) = JsonConvert.DeserializeObject(Of List(Of LocationData))(json)
  12.         Dim DT As New DataTable()
  13.         '// Add Columns to DataTable.
  14.         With DT.Columns
  15.             .Add("zipCode", GetType(String))
  16.             .Add("subDistrictId", GetType(String))
  17.             .Add("subDistrictName", GetType(String))
  18.             .Add("districtId", GetType(String))
  19.             .Add("districtName", GetType(String))
  20.             .Add("provinceId", GetType(String))
  21.             .Add("provinceName", GetType(String))
  22.         End With
  23.         '// Loop for add row data to DataTable.
  24.         Try
  25.             For Each LocationData In dataList
  26.                 For Each subDistrict In LocationData.subDistrictList
  27.                     Dim row As DataRow = DT.NewRow()
  28.                     row("zipCode") = LocationData.zipCode
  29.                     row("subDistrictId") = subDistrict.subDistrictId
  30.                     row("subDistrictName") = subDistrict.subDistrictName
  31.                     '//
  32.                     Dim matchingDistrict = LocationData.districtList.FirstOrDefault(Function(d) d.districtId = subDistrict.districtId)
  33.                     If matchingDistrict IsNot Nothing Then
  34.                         row("districtId") = matchingDistrict.districtId
  35.                         row("districtName") = matchingDistrict.districtName
  36.                     End If
  37.                     '//
  38.                     Dim matchingProvince = LocationData.provinceList.FirstOrDefault(Function(p) p.provinceId = subDistrict.provinceId)
  39.                     If matchingProvince IsNot Nothing Then
  40.                         row("provinceId") = matchingProvince.provinceId
  41.                         row("provinceName") = matchingProvince.provinceName
  42.                     End If
  43.                     DT.Rows.Add(row)
  44.                 Next
  45.             Next
  46.         Catch ex As Exception
  47.             MessageBox.Show(ex.Message)
  48.         End Try
  49.         '// Set DataTable as the DataSource for BindingSource.
  50.         JsonSource.DataSource = DT
  51.         '// Set BindingSource as the DataSource for DataGridView.
  52.         dgvData.DataSource = JsonSource
  53.         '// Remove some columns.
  54.         With dgvData.Columns
  55.             .Remove("subDistrictId")
  56.             .Remove("districtId")
  57.             .Remove("provinceId")
  58.         End With
  59.         '//
  60.         With dgvData
  61.             .Columns("zipCode").HeaderText = "รหัสไปรษณีย์"
  62.             .Columns("subDistrictName").HeaderText = "ตำบล/แขวง"
  63.             .Columns("districtName").HeaderText = "อำเภอ/เขต"
  64.             .Columns("provinceName").HeaderText = "จังหวัด"
  65.         End With
  66.         Call SetupGridView()
  67.         Me.lblRecordCount.Text = "Total: " & Format(dgvData.RowCount, "#,##") & " Records."
  68.     End Sub

  69.     ' / --------------------------------------------------------------------------------
  70.     '// Filter data.
  71.     ' / --------------------------------------------------------------------------------
  72.     Private Sub txtFilterJson_TextChanged(sender As Object, e As System.EventArgs) Handles txtFilterJson.TextChanged
  73.         If rdoFilterAll.Checked Then
  74.             '// Filter for All.
  75.             JsonSource.Filter = _
  76.                 " provinceName LIKE " & "'%" & txtFilterJson.Text & "%'" & _
  77.                 " OR districtName LIKE " & "'%" & txtFilterJson.Text & "%'" & _
  78.                 " OR subDistrictName LIKE " & "'%" & txtFilterJson.Text & "%'" & _
  79.                 " OR zipCode LIKE " & "'%" & txtFilterJson.Text & "%'"
  80.             '// Filter ProvinceName.
  81.         ElseIf rdoProvinceName.Checked Then
  82.             JsonSource.Filter = "provinceName LIKE " & "'%" & txtFilterJson.Text & "%'"
  83.             '// Filter DistrictName.
  84.         ElseIf rdoDistrictName.Checked Then
  85.             JsonSource.Filter = "districtName LIKE " & "'%" & txtFilterJson.Text & "%'"
  86.             '// Filter SubDistrictName.
  87.         ElseIf rdoSubDistrictName.Checked Then
  88.             JsonSource.Filter = "subdistrictName LIKE " & "'%" & txtFilterJson.Text & "%'"
  89.             '// Filter ZipCode.
  90.         ElseIf rdoZipCode.Checked Then
  91.             JsonSource.Filter = "zipCode LIKE " & "'%" & txtFilterJson.Text & "%'"
  92.         End If
  93.         '//
  94.         Me.lblRecordCount.Text = "Total: " & Format(dgvData.RowCount, "#,##") & " Records."
  95.     End Sub

  96.     Private Sub rdoFilterAll_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdoFilterAll.CheckedChanged
  97.         txtFilterJson.Focus()
  98.     End Sub

  99.     Private Sub rdoProvinceName_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rdoProvinceName.CheckedChanged
  100.         txtFilterJson.Focus()
  101.     End Sub

  102.     Private Sub rdoDistrictName_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoDistrictName.CheckedChanged
  103.         txtFilterJson.Focus()
  104.     End Sub

  105.     Private Sub rdoSubDistrictName_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoSubDistrictName.CheckedChanged
  106.         txtFilterJson.Focus()
  107.     End Sub

  108.     Private Sub rdoZipCode_CheckedChanged(sender As Object, e As System.EventArgs) Handles rdoZipCode.CheckedChanged
  109.         txtFilterJson.Focus()
  110.     End Sub

  111. #Region "DATAGRIDVIEW"
  112.     '// Initialized DataGridView.
  113.     Private Sub SetupGridView()
  114.         With dgvData
  115.             .RowHeadersVisible = True
  116.             .AllowUserToAddRows = False
  117.             .AllowUserToDeleteRows = False
  118.             .AllowUserToResizeRows = False
  119.             .MultiSelect = False
  120.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  121.             .ReadOnly = True
  122.             '// Data rows
  123.             .Font = New Font("Tahoma", 10)
  124.             .RowTemplate.MinimumHeight = 27
  125.             .RowTemplate.Height = 27
  126.             '// Autosize Column
  127.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  128.             '// Header
  129.             With .ColumnHeadersDefaultCellStyle
  130.                 .BackColor = Color.RoyalBlue
  131.                 .ForeColor = Color.White
  132.                 .Font = New Font(dgvData.Font, FontStyle.Bold)
  133.             End With
  134.             .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
  135.             .ColumnHeadersHeight = 36
  136.             '/ Accept changes to the header's background color.
  137.             .EnableHeadersVisualStyles = False
  138.             '// Even-Odd Color of Rows.
  139.             .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
  140.             '// Even-Odd Color of Columns.
  141.             For iCol As Integer = 0 To dgvData.Columns.Count - 1
  142.                 '// If any integer Mod by 2 and gets the answer is 0 so even number, 1 is an odd number.
  143.                 If iCol Mod 2 = 1 Then
  144.                     dgvData.Columns(iCol).HeaderCell.Style.BackColor = Color.BlueViolet
  145.                 Else
  146.                     dgvData.Columns(iCol).HeaderCell.Style.BackColor = Color.SeaGreen
  147.                 End If
  148.             Next
  149.         End With
  150.     End Sub
  151. #End Region

  152. #Region "FUNCTION"
  153.     ' / --------------------------------------------------------------------------------
  154.     ' / Get my project path
  155.     ' / AppPath = C:\My Project\bin\debug
  156.     ' / Replace "\bin\debug" with ""
  157.     ' / Return : C:\My Project\
  158.     Function MyPath(ByVal AppPath As String) As String
  159.         '/ Return Value
  160.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  161.         '// If not found folder then put the \ (BackSlash) at the end.
  162.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  163.     End Function
  164. #End Region

  165. End Class

  166. '// Define data model classes.
  167. Public Class SubDistrict
  168.     Public Property subDistrictId As String
  169.     Public Property districtId As String
  170.     Public Property provinceId As String
  171.     Public Property subDistrictName As String
  172. End Class

  173. Public Class District
  174.     Public Property districtId As String
  175.     Public Property districtName As String
  176.     Public Property provinceId As String
  177. End Class

  178. Public Class Province
  179.     Public Property provinceId As String
  180.     Public Property provinceName As String
  181. End Class

  182. Public Class LocationData
  183.     Public Property zipCode As String
  184.     Public Property subDistrictList As List(Of SubDistrict)
  185.     Public Property districtList As List(Of District)
  186.     Public Property provinceList As List(Of Province)
  187. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...


ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-30 00:44 , Processed in 0.188725 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้