• Abaqus (0)
  • Questions (1)
Ask a Question

Recent Posts

  • Thay thế New Line từ Clipboard với batch file (Window)
  • Macro Hypermesh tạo comp đối xứng, Tạo comps mới full thuộc tính như comps ban đầu
  • JSON vs VBA

Recent Comments

  • admin on Protected: chatchit
  • admin on Protected: chatchit
  • tt on Protected: chatchit
  • tt on Protected: chatchit
  • admin on Protected: chatchit
Tí Tẹo
  • Home
  • Community
  • Documents
  • CAE
    • Vật Liệu
    • Toán
  • Skill
  • Giải Trí
No Result
View All Result
  • Login
  • Register

Notifications

  • JSON vs VBA
    5 months
  • JSON vs VBA
    5 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    5 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    5 months
  • JSON vs VBA
    9 months
  • Macro Hypermesh tạo comp đối xứng, Tạo comps mới full thuộc tính như comps ban đầu
    9 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    10 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    10 months
  • JSON vs VBA
    12 months
  • JSON vs VBA
    12 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • JSON vs VBA
    1 year
  • JSON vs VBA
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Macro Hypermesh tạo comp đối xứng, Tạo comps mới full thuộc tính như comps ban đầu
    1 year
  • Macro Hypermesh tạo comp đối xứng, Tạo comps mới full thuộc tính như comps ban đầu
    1 year
  • Show older
Tí Tẹo
  • Home
  • Community
  • Documents
  • CAE
    • Vật Liệu
    • Toán
  • Skill
  • Giải Trí
No Result
View All Result
  • Login
  • Register

Notifications

  • JSON vs VBA
    5 months
  • JSON vs VBA
    5 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    5 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    5 months
  • JSON vs VBA
    9 months
  • Macro Hypermesh tạo comp đối xứng, Tạo comps mới full thuộc tính như comps ban đầu
    9 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    10 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    10 months
  • JSON vs VBA
    12 months
  • JSON vs VBA
    12 months
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • JSON vs VBA
    1 year
  • JSON vs VBA
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Thay thế New Line từ Clipboard với batch file (Window)
    1 year
  • Macro Hypermesh tạo comp đối xứng, Tạo comps mới full thuộc tính như comps ban đầu
    1 year
  • Macro Hypermesh tạo comp đối xứng, Tạo comps mới full thuộc tính như comps ban đầu
    1 year
  • Show older
Tí Tẹo
No Result
View All Result

JSON vs VBA

Hướng dẫn xử lý chuỗi JSON bằng VBA trên excel cho người mới bắt đầu

0
JSON vs VBA

JSON vs VBA

JSON là một kiểu định dạng dữ liệu khá là phổ biến và đã có các thư viện hỗ trợ xử lý với các ngôn ngữ lập trình WEB, nhưng để dùng VBA để đọc và lấy dữ liệu thì còn khá là vất vả.

Qua bài viết này, Tí Tẹo sẽ chia sẻ một phương pháp đơn giản để có thể xử lý được chuỗi JSON bằng VBA ngay cả người mới bắt đầu cũng có thể dễ dàng nắm bắt.

Đầu tiên cần hiểu JSON là gì ?

Json (JavaScript Object Notation) hiểu đơn giản

  • Là một kiểu định dạng dữ liệu văn bản thuần túy,
  • Được viết theo một quy luật nhất định
  • Sử dụng các cặp Key-Value đặt trong dấu ngoặc kép “”
  • Được bao lại bởi dấu ngoặc {}
  • Ngăn cách giữa các giữ liệu bằng dấu “,”

Giờ cùng thử đi phân tích chuỗi JSON ví dụ dưới đây bằng VBA

{
    "Tieu De": "Thuc Pham Hang Ngay",
    "chi phi": [50, 100, 5, 10],
    "Chi Tiet": {
        "Thu 2": {
                "Rau": "cai bap",
                "trang mieng" : {
                    "hoa qua" : "tao",
                 } ''''
                "thit": "thit bo"
            },
        "Thu 2": {
                "Rau": "rau muong",
                "thit": "thit ga"
            },
        "Thu 4": "My tom",
        "Thu 5": "chua biet"
    }
}

Đối với một chuỗi JSON chuẩn thì các giá trị, từ khóa được đặt trong dấu ngoặc kép “”, vì vậy để khai báo được chuỗi JSON trong VBA thì khai báo như một chuỗi bình thường, và nhân đôi dấu ngoặc kép lên

" ⇒""

Vậy với biến chuỗi strData và từ khóa cần lấy giá trị strKey sẽ được khai báo như sau:

Dim strData As String, strKey As String

strData = "{""Tieu De"":""Thuc Pham Hang Ngay"",""chi phi"":[50,100,5,10],""Chi Tiet"":{""Thu 2"":{""Rau"": ""cai bap"",""trang mieng"" : { ""hoa qua"" : ""tao"",}""thit"": ""thit bo""},""Thu 2"":{""Rau"":""rau muong"",""thit"":""thit ga""},""Thu 4"":""My tom"",""Thu 5"":""chua biet""}}"
strKey = """Thu 2"""

Kết quả mong muốn có được với mỗi từ khóa Key sẽ lấy được giá trị VALUE tương ứng

Dưới đây mình chia sẻ code, các bạn có thể xem video trên youtube để hiểu rõ hoạt động của CODE này nhé

Function FnJSON(strData As String, strKey As String) As Dictionary
    Dim Arr As Variant, NuCount As Long
    Dim strtemp As String
    Dim NuStart As Long, Nulength As Long
    Dim i As Long, continue As Boolean
    Dim strKeyOpen As String, strKeyClose As String
    Dim skip As Boolean
    
    Dim JsonKey As String
    Dim JsonDic As New Scripting.Dictionary
    JsonDic.CompareMode = vbTextCompare
    strData = VBA.Replace(VBA.Replace(VBA.Replace(VBA.Replace(strData, VBA.vbCr, ""), VBA.vbLf, ""), VBA.vbTab, ""), VBA.vbCrLf, "")
    Arr = Split(strData, strKey)
    For NuCount = 1 To UBound(Arr)
        skip = False
        strtemp = Replace(Arr(NuCount), " ", "")
        strKeyOpen = Mid(strtemp, InStr(strtemp, ":") + 1, 1)
        Select Case strKeyOpen
            Case "{"
                strKeyClose = "}"
            Case "["
                strKeyClose = "]"
            Case Else
                strKeyClose = ","
                skip = True
        End Select
        
        NuStart = 2
        For i = 0 To NuCount - 1
            NuStart = NuStart + Len(Arr(i)) + Len(strKey)
        Next i
        
        Nulength = 0
        Do
            Nulength = InStr(NuStart + Nulength, strData, strKeyClose) - NuStart + 1
            strtemp = Mid(strData, NuStart, Nulength)
            continue = True
            If Not skip Then
                continue = (Fn_Count(strtemp, strKeyOpen) - Fn_Count(strtemp, strKeyClose) = 0)
            Else
                strtemp = Mid(strtemp, 1, Len(strtemp) - 1)
                strtemp = Replace(strtemp, """", "")
            End If
        Loop Until (continue)
            'Debug.Print strtemp
            JsonKey = "[" & Replace(strKey, """", "") & "][" & NuCount & "]"
            JsonKey = strKey & "[" & NuCount & "]"
            If Not JsonDic.Exists(JsonKey) Then
                JsonDic.Add JsonKey, New Collection
                JsonDic(JsonKey).Add item:=strtemp, key:=JsonKey
            Else
                JsonDic(JsonKey).Add item:=strtemp, key:=JsonKey
            End If
    Next NuCount
    Set FnJSON = Nothing
    Set FnJSON = JsonDic
    Set JsonDic = Nothing
End Function
Function Fn_Count(str As String, key As String) As Long
    Dim a As Long, b As Long
    Dim strtemp As String
    
    a = Len(str)
    strtemp = Replace(str, key, "")
    b = Len(strtemp)
    Fn_Count = a - b
    
End Function

Related Posts

No Content Available

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Home
  • Community
  • Documents
  • CAE
  • Skill
  • Giải Trí

© 2021 Tí Tẹo

No Result
View All Result
  • Home
  • Community
  • Documents
  • CAE
    • Vật Liệu
    • Toán
  • Skill
  • Giải Trí

© 2021 Tí Tẹo

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In