如何以特定格式将Excel导出为JSON

时间:2019-11-08 11:43:58

标签: json excel vba excel-formula

我正在使用以下代码:

Private Sub CommandButton1_Click()   

Dim excelRange As Range

Dim jsonItems As New Collection

Dim jsonDictionary As New Dictionary

Dim jsonFileObject As New FileSystemObject

Dim jsonFileExport As TextStream

Dim i As Long

Dim cell As Variant


Set excelRange = Sheet1.Cells(3, 3).CurrentRegion

For i = 4 To excelRange.Rows.Count

    jsonDictionary("ID") = Sheet1.Cells(i, 1)

    jsonDictionary("Pass") = Sheet1.Cells(i, 2)

    jsonDictionary("MN") = Sheet1.Cells(i, 3)

    jsonDictionary("Email") = Sheet1.Cells(i, 4)

    jsonItems.Add jsonDictionary

    Set jsonDictionary = Nothing

Next i

MsgBox (JsonConverter.ConvertToJson(jsonItems, Whitespace:=0))

Set jsonFileExport = jsonFileObject.CreateTextFile(Application.GetSaveAsFilename(fileFilter:="Text Files (*.json), *.json"), True)

jsonFileExport.WriteLine (JsonConverter.ConvertToJson(jsonItems, Whitespace:=3))

我得到了以下格式的JSON文件:

[
   {
      "id": "AA2478FV",

      "PAss": "MAAS",

      "MN": "9878965421",

      "email": "",Abc@123
}]

但是我需要以下格式的JSON:

{"b2b deatils":[{"id":"01AF2578T","pass":"N","MN":8789755654,"furdetails":[{"nameorg":

[{"emp":1,"itm_det":{"recmount":0}}]}]

我怎么能得到这个?

0 个答案:

没有答案