51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

将VBA-WEB字典的格式调整为JSON主体。

英文:

Format Adjustment for VBA-WEB dictionary to JSON Body

问题 {#heading}

{
  "applicationId": "customsHub",
  "exportAsContentType": "NOT_ZIPPED_SINGLE",
  "documents": 
  {
    "documentIds": 
    [
      {
        "documentId": "491e2229-471a-4b18-907a-0e25a312e5a7"
      }
    ]
  }
}

英文:

I asked a question before, but I didn't ask it well... so after I did some more homework I realised I need help with the conversion of VBA dictionary into a specific JSON format to be sent as the Body of an API POST query.

Here is what I have:

Dim Body As New Dictionary

Body.Add "applicationId", "customsHub" Body.Add "exportAsContentType", "NOT_ZIPPED_SINGLE" Body.Add "documents", New Dictionary

With Body("documents") .Add "documentIds", New Dictionary

With Body("documents")("documentIds")
.Add "documentId", "491e2229-471a-4b18-907a-0e25a312e5a7"
End With

End With


Output:

{
  "applicationId": "customsHub",
  "exportAsContentType": "NOT_ZIPPED_SINGLE",
  "documents": {
    "documentIds": {
      "documentId": "491e2229-471a-4b18-907a-0e25a312e5a7"
    }
  }

What I need to get -

{
  "applicationId": "customsHub",
  "exportAsContentType": "NOT_ZIPPED_SINGLE",
  "documents": 
  {
    "documentIds": 
    [
      {
        "documentId": "491e2229-471a-4b18-907a-0e25a312e5a7"
      }
    ]
  }
}

How do I add this nested array as part of the VBA dictionary pre-conversion?

答案1 {#1}

得分: 1

在Json中,"[]"字符表示一个数组。因此,你应该在一个数组内创建最后一个New Dictionary

Sub createDict_REturnJson()
  Dim Body As New scripting.Dictionary

Body.Add "applicationId", "customsHub" Body.Add "exportAsContentType", "NOT_ZIPPED_SINGLE" Body.Add "documents", New scripting.Dictionary

With Body("documents") .Add "documentIds", Array(New scripting.Dictionary) '将字典放入一个数组中

With Body("documents")("documentIds")(0) '将项放置在数组的第一个元素中:
.Add "documentId", "491e2229-471a-4b18-907a-0e25a312e5a7"
End With

End With

Dim Json As String '用于测试输出: Json = JsonConverter.ConvertToJson(Body, 2) '假设你使用JsonConverter模块... Debug.Print Json End Sub


为了更好地理解,你可以用下面的代码替换这部分:

  With Body("documents")
    .Add "documentIds", Array(New scripting.Dictionary) '将字典放入一个数组中
With Body("documents")("documentIds")(0) '将项放置在数组的第一个元素中:
  .Add "documentId", "491e2229-471a-4b18-907a-0e25a312e5a7"
End With    

End With


使用下面的代码替代,将两个字典添加到数组中,第一个字典包含两个键(IDs),第二个字典仅包含一个键(仅用于教学目的...):

  With Body("documents")
    .Add "documentIds", Array(New scripting.Dictionary, New scripting.Dictionary) '将两个字典放入数组中
With Body("documents")("documentIds")(0)
    .Add "documentId", "491e2229-471a-4b18-907a-0e25a312e5a7" '第一个字典的第一个键
    .Add "documentId2", "testID2" '第一个字典的第二个键
End With
Body("documents")("documentIds")(1).Add "DocumentID3", "TestID3" '第二个字典的第一个(单一)键...

End With


英文:

"[]" characters in Json means an array. So you should create the last New Dictionary inside an array:

Sub createDict_REturnJson()
  Dim Body As New scripting.Dictionary

Body.Add "applicationId", "customsHub" Body.Add "exportAsContentType", "NOT_ZIPPED_SINGLE" Body.Add "documents", New scripting.Dictionary

With Body("documents") .Add "documentIds", Array(New scripting.Dictionary) 'place the dictionary in an array

With Body("documents")("documentIds")(0) 'place the item as the array first element:
.Add "documentId", "491e2229-471a-4b18-907a-0e25a312e5a7"
End With

End With

Dim Json As String ' to test the output: Json = JsonConverter.ConvertToJson(Body, 2) 'supposing that you use JsonConverter module... Debug.Print Json End Sub


To better catch the idea you can replace this part:

  With Body("documents")
    .Add "documentIds", Array(New scripting.Dictionary) 'place the dictionary in an array
With Body("documents")("documentIds")(0) 'place the item as the array first element:
  .Add "documentId", "491e2229-471a-4b18-907a-0e25a312e5a7"
End With    

End With


with the next one, adding two dictionaries in the array, the first one keeping two keys (IDs) and the second one only one (only for didactic purpose...):

  With Body("documents")
    .Add "documentIds", Array(New scripting.Dictionary, New scripting.Dictionary) 'placing two dictionaries in the array
With Body("documents")("documentIds")(0)
    .Add "documentId", "491e2229-471a-4b18-907a-0e25a312e5a7" 'first dictionary first key
    .Add "documentId2", "testID2" 'first dictionary second key
End With
Body("documents")("documentIds")(1).Add "DocumentID3", "TestID3" 'second dictionary first (single) key...

End With



赞(2)
未经允许不得转载:工具盒子 » 将VBA-WEB字典的格式调整为JSON主体。