我一直在使用JsonConverter決議JSON檔案,然后使用以下代碼將所有“標簽”和子“值”提取dictionary到我的 userform 中combobox。
Set JSP = JsonConverter.ParseJson(JSONtxtString)
For Each A In JSP
Debug.Print A
If Not IsObject(JSP(A)) Then
Debug.Print JSP(A)
Else
For Each B In JSP(A)
If Not IsObject(JSP(B)) Then
'Debug.Print B("label")
Me.AttributeComBo.AddItem B("label")
Me.ConditionBox.AddItem B("label")
If B("type") = "selectboxes" Or B("type") = "select" Then
For Each C In B("values")
'Debug.Print C("label")
Me.CBSubValues.AddItem C("label")
Next C
End If
Else
End If
Next B
End If
Next A
但是我無法從“喜歡的顏色”中獲取值(“標簽”)。來自以下 JSON。
{"components": [
{
"label": "Family Name",
"tableView": true,
"key": "familyName",
"type": "textfield",
"input": true
},
{
"label": "Amount of Money",
"mask": false,
"tableView": false,
"delimiter": false,
"requireDecimal": false,
"inputFormat": "plain",
"truncateMultipleSpaces": false,
"key": "amountOfMoney",
"type": "number",
"input": true
},
{
"label": "I hereby confirm",
"tableView": false,
"key": "iHerebyConfirm",
"type": "checkbox",
"input": true,
"defaultValue": false
},
{
"label": "Which Cities do you like",
"optionsLabelPosition": "right",
"tableView": false,
"values": [
{
"label": "New York",
"value": "newNew YorkYork",
"shortcut": ""
},
{
"label": "Munich",
"value": "Munich",
"shortcut": ""
},
{
"label": "Paris",
"value": "Paris",
"shortcut": ""
},
{
"label": "Hongkong",
"value": "Hongkong",
"shortcut": ""
},
{
"label": "Mumbai",
"value": "Mumbai",
"shortcut": ""
}
],
"key": "whichCitiesDoYouLike",
"type": "selectboxes",
"input": true,
"inputType": "checkbox"
},
{
"label": "Favorite color",
"widget": "choicesjs",
"tableView": true,
"data": {
"values": [
{
"label": "black",
"value": "black"
},
{
"label": "white",
"value": "white"
},
{
"label": "blue",
"value": "blue"
},
{
"label": "green",
"value": "green"
}
]
},
"key": "favoriteColor",
"type": "select",
"input": true
},
{
"type": "button",
"label": "Submit",
"key": "submit",
"disableOnInvalid": true,
"input": true,
"tableView": false
}
]
}
我無法從標簽中獲取值,即黑色、白色、藍色、綠色(“最喜歡的顏色”) 從任何 JSON 檔案中提取所有值的更好解決方案是什么?如何遍歷 JSON 中的每個物件并提取所有值?
uj5u.com熱心網友回復:
在 VBA 中使用 JSON 可能令人沮喪。我創建了這個答案,用作我自己的參考。但更重要的是,花時間清楚地確定 JSON 的哪些部分被參考。
在您的情況下,頂級是 a Collectionof components。每個組件都有一個label. 一個組件可能有一個values.
但是,在“最喜歡的顏色”組件的情況下,values它們位于data Dictionary.
這是我用來保持清晰和直接的一些示例代碼:
Option Explicit
Sub test2(ByRef jsonText As String)
Dim json As Variant
Set json = JsonConverter.ParseJson(jsonText)
Dim components As Collection
Set components = json("components")
Dim component As Variant
For Each component In components
Dim label As String
label = component("label")
Debug.Print "Label: " & label
On Error Resume Next
Dim values As Collection
Set values = component("values")
Dim data As Dictionary
Set data = component("data")
On Error GoTo 0
Dim value As Variant
If Not values Is Nothing Then
For Each value In values
Debug.Print " Value: " & value("label")
Next value
ElseIf Not data Is Nothing Then
Set values = data("values")
For Each value In values
Debug.Print " Value: " & value("label")
Next value
Else
Debug.Print " No values"
End If
Set values = Nothing
Next component
End Sub
請注意,values不能保證每個component. 這就是我們必須禁用/啟用錯誤檢查的原因。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/352928.html
上一篇:合并/合并頂級字典
