以特定方式在Excel中解析JSON嵌套数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以特定方式在Excel中解析JSON嵌套数组相关的知识,希望对你有一定的参考价值。

enter image description here我得到了一些帮助,但不确定是应该在原始问题中继续进行还是开始新的问题,因为从技术上讲,这是一个新问题(我选择了新问题,但请让我知道你们接下来会喜欢什么时间)。

我想出了如何将标准信息集解析为不同的列。我在解析嵌套数组时遇到麻烦。当到达代码的那一部分时,它给出一个运行时错误5]customfields下的数组需要解析为列,但估计行数组包含需要解析为行的零件/定价信息。

这是我到目前为止编写的代码:

Sub doJSON()

    Dim JSON As Object
    ' Dim CustomFields As Dictionary
    Dim FSO As FileSystemObject
    Dim TS As TextStream
    Dim strJSON As String
    Dim ws As Worksheet
    Dim i As Long
    Dim Item As Variant

    Set FSO = New FileSystemObject
    Set TS = FSO.OpenTextFile("C:UsersjbVBVA Team DropboxY&M Owner Filesauto update vba macrosb data.txt")
    strJSON = TS.ReadAll
    Set JSON = ParseJson(strJSON)
    Set ws = Worksheets("Sheet20")
    i = 3
    ws.Cells(2, 1) = "WorkOrderNumber"
    ws.Cells(2, 2) = "EstimateNumber"
    ws.Cells(2, 3) = "EstimateDate"
    ws.Cells(2, 4) = "WonOrLostDate"
    ws.Cells(2, 5) = "ScheduledTime"
    ws.Cells(2, 6) = "EstimatedDuration"
    ws.Cells(2, 7) = "Customer Name"
    ws.Cells(2, 8) = "Contact name"
    ws.Cells(2, 9) = "Location Name"
    ' ws.Cells(2, 10) = "GeoCoordinates : Latitude"
    ' ws.Cells(2, 11) = "GeoCoordinates : Longitude"
    ws.Cells(2, 12) = "MarketingCampaign"
    ws.Cells(2, 13) = "Team"
    ws.Cells(2, 14) = "WorkOrderDate"
    ws.Cells(2, 15) = "DateFinished"
    ws.Cells(2, 16) = "ScheduledTime"
    ws.Cells(2, 17) = "EstimatedDuration"
    ws.Cells(2, 18) = "Notes"
    ws.Cells(2, 19) = "PrivateNotes"
    ws.Cells(2, 20) = "SalesRepresentative"
    ws.Cells(2, 21) = "Description"
    ws.Cells(2, 22) = "Status"
    ws.Cells(2, 23) = "IsInvoiced"
    ws.Cells(2, 24) = "CreatedBy"
    ws.Cells(2, 25) = "CreatedOn"
    ws.Cells(2, 26) = "UpdatedOn"
    ws.Cells(2, 27) = "UpdatedBy"
    ws.Cells(2, 28) = "Version"
    ws.Cells(2, 29) = "CC Referred By"
    For Each Item In JSON("Data")
        ws.Cells(i, 1) = Item("WorkOrderNumber")
        ws.Cells(i, 2) = Item("EstimateNumber")
        ws.Cells(i, 3) = Item("EstimateDate")
        ws.Cells(i, 4) = Item("WonOrLostDate")
        ws.Cells(i, 5) = Item("ScheduledTime")
        ws.Cells(i, 6) = Item("EstimatedDuration")
        ws.Cells(i, 7) = Item("Customer")("Name")
        ws.Cells(i, 8) = Item("Conact")("Name")
        ws.Cells(i, 9) = Item("Location")("Name")
' THis next 2 line gives me a type mismatch error and i cant figure out why
        ' ws.Cells(i, 10) = Item("GeoCoordinates")("Latitude")
        ' ws.Cells(i, 11) = Item("GeoCoordinates")("Longitude")
        ws.Cells(i, 12) = Item("MarketingCampaign")("Name")
        ws.Cells(i, 13) = Item("Team")("name")
        ws.Cells(i, 14) = Item("WorkOrderDate")
        ws.Cells(i, 15) = Item("DateFinished")
        ws.Cells(i, 16) = Item("ScheduledTime")
        ws.Cells(i, 17) = Item("EstimatedDuration")
        ws.Cells(i, 18) = Item("Notes")
        ws.Cells(i, 19) = Item("PrivateNotes")
        ws.Cells(i, 20) = Item("SalesRepresentative")("Name")
        ws.Cells(i, 21) = Item("Description")
        ws.Cells(i, 22) = Item("Status")
        ws.Cells(i, 23) = Item("IsInvoiced")
        ws.Cells(i, 24) = Item("Metadata")("CreatedBy")
        ws.Cells(i, 25) = Item("Metadata")("CreatedOn")
        ws.Cells(i, 26) = Item("Metadata")("UpdatedOn")
        ws.Cells(i, 27) = Item("Metadata")("UpdatedBy")
        ws.Cells(i, 28) = Item("Metadata")("Version")
        i = i + 1
    Next
    For Each Item In JSON("Data")("CustomFields")
        ' this is the part of the code that gives me a runtime error 5 . i need it to pull name data from custom field
        ws.Cells(i, 29) = Item("name")(1)
        i = i + 1
    Next
    ' this next part of the code i dont even know where to start.i need to add the material and price information for each work order item and repeat all the other information like work order number for each different material/price added to the table.

End Sub

我提供了json文件的示例以及我希望最终结果看起来像的示例:

{
    "TotalCount": 5136,
    "Data": [
        {
            "WorkOrderId": null,
            "WorkOrderNumber": null,
            "Id": 24668520,
            "Metadata": {
                "CreatedBy": "**blah**.com",
                "CreatedOn": "2020-01-17T21:02:11",
                "UpdatedOn": "2020-01-27T14:36:42",
                "UpdatedBy": "blah.com",
                "Version": 6
            },
            "ExternalSystemId": null
        },
        {
            "EstimateNumber": "EST 12523",
            "Customer": {
                "Inactive": false,
                "Id": 8656822,
                "Name": "blahe"
            },
            "Location": {
                "Id": 9314838,
                "Name": "Primary"
            },
            "GeoCoordinates": {
                "Latitude": 36.7926476,
                "Longitude": -76.25019759999998
            },
            "CoordinatesCaptured": true,
            "Contact": {
                "Id": 9111128,
                "Name": "blah"
            },
            "ThirdPartyBillPayer": null,
            "IsMarketingCampaignSetByLead": false,
            "MarketingCampaign": {
                "Id": 115753,
                "Name": "Web Call In"
            },
            "JobCategory": null,
            "SalesRepresentative": {
                "Id": 242299,
                "Name": "**blah**"
            },
            "DefaultEquipment": null,
            "Description": "Guard Estimate",
            "Status": "Finished",
            "Branch": {
                "Id": 145945,
                "Name": "blah-VA"
            },
            "Team": {
                "Id": 188466,
                "Name": "blah"
            },
            "TeamMembers": [
                {
                    "EmployeeId": 242299,
                    "UserId": 179578,
                    "FirstName": "blah",
                    "LastName": "blah",
                    "Email": "blah.com",
                    "Title": null
                }
            ],
            "ConfirmationStatus": "None",
            "EstimateDate": "2020-01-21T00:00:00",
            "WonOrLostDate": null,
            "ScheduledTime": 930,
            "EstimatedDuration": 60,
            "ArrivalWindow": 60,
            "EarliestArrival": null,
            "LatestDeparture": null,
            "Notes": "
GUTTER - NO EXISTING GUTTERS
Client requests new 5" ( white) seamless gutters and downspouts to prevent further soil erosion and water damage to foundation of home, caused by lack of functioning gutter system. 5 year labor and 20 year materials warranty included.",
            "PrivateNotes": "Solutionist Instructions: 1/17/20- Gutter Guard Estimate- A lot of trees around him he cant keep up with the cleaning- he needs 3:30 or after he runs a daycare- TB
Llyods e-mail keeps bouncing back we confirmed its correct still not going thru- he uses (2) -(o) in his first name but (1) -(o) in his e-mail- I sent text confirmation e-mail- TB

House Color: Orange Brick 
Does Customer Have Dogs: YES- small

Date Estimate Scheduled & Email Sent:

Scheduling Instructions: 

Date Called & Emailed to Schedule:
Date of Service Text & Email: 

Billing Instructions:
Deposit $ 
Balance Due $ 

Installer Instructions: 


Installer Notes:
PROS:

CONS:

WAS PAYMENT TAKEN?",
            "InvoiceNotes": null,
            "ReminderType": "None",
            "ReminderValue": 0,
            "ReminderMessage": "",
            "EstimateLines": [
                {
                    "Id": 30130758,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688230,
                        "SKU": "2X3­PO­ALU",
                        "Name": "2 3/4" Aluminum Pop Outlet",
                        "Type": "Product"
                    },
                    "Price": 1,
                    "Quantity": 11,
                    "Description": "Pop Outlets are snapped into the gutter opening that leads into the downspout. The downspout is secured directly to the outlet.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130757,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688633,
                        "SKU": "5K­HH­ALU",
                        "Name": "5" Aluminum Hidden Hanger",
                        "Type": "Product"
                    },
                    "Price": 2,
                    "Quantity": 120,
                    "Description": "Hidden Hangers are used to secure a gutter to a structure.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130755,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688648,
                        "SKU": "5K­ISM­ALU­ WHI",
                        "Name": "5" Aluminum White K-Style Inside Strip Miter",
                        "Type": "Product"
                    },
                    "Price": 20,
                    "Quantity": 3,
                    "Description": "An inside strip miter is used to create a smooth transition between two gutters that intersect at a 90░ angle in the inside valley of a structure.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30131039,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688246,
                        "SKU": "2X3-­BE­-ALU­-WHT",
                        "Name": "2x3" Aluminum White B Elbow",
                        "Type": "Product"
                    },
                    "Price": 5,
                    "Quantity": 4,
                    "Description": "B Elbows are typically used to attach a downspout to the side of a structure. They are also used on other areas of a downspout enabling it to conform to the contour of a structure.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30131036,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688646,
                        "SKU": "5K­IBYM­ALU­ WHI",
                        "Name": "5" Aluminum White K-Style Inside Bay Miter",
                        "Type": "Product"
                    },
                    "Price": 20,
                    "Quantity": 1,
                    "Description": "An inside bay miter is used to create a smooth transition between two gutters that intersect at a 45░ angle in the inside valley of a structure.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30131051,
                    "ParentId": null,
                    "Inventory": {
                        "Id": 1688793,
                        "SKU": "ONE-24-GUARD-KIT",
                        "Name": "Items for Installation",
                        "Type": "Bundle"
                    },
                    "Price": 2220,
                    "Quantity": 1,
                    "Description": "See product details below",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30131052,
                    "ParentId": 30131051,
                    "Inventory": {
                        "Id": 1688663,
                        "SKU": "5­ONE­24­ GUARD",
                        "Name": "5'' One Gutter Guard",
                        "Type": "Product"
                    },
                    "Price": 12,
                    "Quantity": 185,
                    "Description": "One Guard gutter protection is an approved gutter guard for The Brothers that Just do Gutters. OneGuard gutter protection is designed with a .027" gauge thick 3105 H14/24 aluminum chassis and painted with polyester paint .7 mil thick (both sides). The One Guard 24 mesh is fabricated from surgical stainless steel 24 mesh with .0075" wire diameter. Self­ supporting with NO substrate, it creates an even higher flow choice. The One Guard Product helps strengthen the entire gutter system. The chassis dam of the One Gutter Guard acts as a "culvert" to capture the bulk of the roof water in the first zone to minimize the migration towards the front of the system. Dam also strengthens the chassis and supports the insert in snow loads or other situations. All of these features make One Gutter Guard an excellent product to keep your gutters free of any interior clog.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30131061,
                    "ParentId": null,
                    "Inventory": {
                        "Id": 1744982,
                        "SKU": "DISC",
                        "Name": "Discount",
                        "Type": "Product"
                    },
                    "Price": -555,
                    "Quantity": 1,
                    "Description": "Gutter and guard discount of $555",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30131054,
                    "ParentId": 30131051,
                    "Inventory": {
                        "Id": 1688764,
                        "SKU": "GTR­CL­ GUARDS",
                        "Name": "Interior Gutter Cleaning ­ with Guard Installation",
                        "Type": "Product"
                    },
                    "Price": 0,
                    "Quantity": 1,
                    "Description": "No charge gutter cleaning with guard installation. Cleaning of the interior of a gutter system, elbows and downspouts either by hand or leaf blower. All leaves and debris will be discarded in nearby woods or completely removed from the premises.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130933,
                    "ParentId": null,
                    "Inventory": {
                        "Id": 1688787,
                        "SKU": "5K-ALU-WHI-KIT",
                        "Name": "Items for Installation",
                        "Type": "Bundle"
                    },
                    "Price": 2019,
                    "Quantity": 1,
                    "Description": "See product details below",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130754,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688650,
                        "SKU": "5K­OBYM­ALU­ WHI",
                        "Name": "5" Aluminum White K-Style Outside Bay Miter",
                        "Type": "Product"
                    },
                    "Price": 20,
                    "Quantity": 1,
                    "Description": "An outside bay miter is used to create a smooth transition between two gutters that intersect at a 45░ angle on the outside corner of a structure.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130756,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688649,
                        "SKU": "5K­LEC­ALU­ WHI",
                        "Name": "5" Aluminum White K-Style Left End Cap",
                        "Type": "Product"
                    },
                    "Price": 2,
                    "Quantity": 11,
                    "Description": "A left end cap is secured to the left side of an open
gutter just past the end of the roof. It is crimped and sealed to prevent leaking.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130759,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688245,
                        "SKU": "2X3­-AE­-ALU­-WHT",
                        "Name": "2x3" Aluminum White A Elbow",
                        "Type": "Product"
                    },
                    "Price": 5,
                    "Quantity": 22,
                    "Description": "A Elbows are typically used to attach a downspoutto the face of a structure. They are also used on other areas of a downspout enabling it to conform to the contour of a structure.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130760,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688247,
                        "SKU": "2X3­-DS­ALU­-WHI",
                        "Name": "2x3" Aluminum White Downspout",
                        "Type": "Product"
                    },
                    "Price": 5,
                    "Quantity": 100,
                    "Description": "Downspouts carry rainwater from the gutter to the ground.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130761,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688645,
                        "SKU": "5K­ALU­WHI",
                        "Name": "5" Aluminum White K-Style Gutter",
                        "Type": "Product"
                    },
                    "Price": 5,
                    "Quantity": 190,
                    "Description": "K­Style Gutters are designed to carry rainwater from the roof to the ground, protecting a structure from water damage. The K­Style design offers optimal performance and is the industry standard for gutter installation. The color is baked into this .032 heavy gauge gutter and comes with a 20­year warranty.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130934,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688653,
                        "SKU": "5K­REC­ALU­ WHI",
                        "Name": "5" Aluminum White K-Style Right End Cap",
                        "Type": "Product"
                    },
                    "Price": 2,
                    "Quantity": 11,
                    "Description": "A right end cap is secured to the right side of an open gutter just past the end of the roof. It is crimped and sealed to prevent leaking.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130935,
                    "ParentId": 30130933,
                    "Inventory": {
                        "Id": 1688250,
                        "SKU": "2X3­UC­ALU­ WHI",
                        "Name": "2x3" Aluminum White U Clips",
                        "Type": "Product"
                    },
                    "Price": 2,
                    "Quantity": 22,
                    "Description": "U Clips are low profile accessories used to secure a downspout to a structure. The U shape design offers low visibility and superior strength.",
                    "IsTaxable": false,
                    "Tax": null
                },
                {
                    "Id": 30130779,
                    "ParentId": 30131051,
                    "Inventory": {
                        "Id": 1688770,
                        "SKU": "ONE­GUARD­ REALCHECK",
                        "Name": "One Guard Reality Check",
                        "Type": "Product"
                    },
                    "Price": 0,
                    "Quantity": 1,
                    "Description": "While many of our clients enjoy maintenance free gutters, it is not the reality for everyone. One Gutter Guard Protection is an amazing product designed to keep the inside of your gutters and downspouts clog free. Guards will freeze & you may get icicles. Rain hitting the gutter surface following the profile of the gutter may cause drip. Debris that remains on the guard will need to be removed from time to time with a broom or leaf blower in order to let the water flow into the guard. Roof pitch, design & types of trees will determine potential maintenance of the guard.",
                    "IsTaxable": false,
                    "Tax": null
                }
            ],
            "CustomFields": [
                {
                    "Name": "CC Referred By",
                    "Value": ""
                },
                {
                    "Name": "E3 Electric:",
                    "Value": ""
                },
                {
                    "Name": "W3 Yard Sign Posted",
                    "Value": ""
                },
                {
                    "Name": "P2 Materials Checked",
                    "Value": ""
                },
                {
                    "Name": "P3 Photos & Diagrams Included",
                    "Value": ""
                },
                {
                    "Name": "W8 Referrals given to Customer",
                    "Value": ""
                },
                {
                    "Name": "E2 Ladders Needed:",
                    "Value": ""
                },
                {
                    "Name": "P4 Priority List",
                    "Value": "False"
                },
                {
                    "Name": "P Material Box",
                    "Value": ""
                },
                {
                    "Name": "E4 Takedown",
                    "Value": ""
                },
                {
                    "Name": "W6 Describe Delays",
                    "Value": ""
                },
                {
                    "Name": "W7 Final walk around/Payment Taken",
                    "Value": ""
                },
                {
                    "Name": "W5 Amount of  Delay Due To Work Obstacles",
                    "Value": ""
                },
                {
                    "Name": "W Tools/Materials visually inspected",
                    "Value": ""
                },
                {
                    "Name": "E Roof Pitch",
                    "Value": ""
                },
                {
                    "Name": "E1 Roof Material ",
                    "Value": ""
                },
                {
                    "Name": "CC1 Marketing Source Correction",
                    "Value": ""
                },
                {
                    "Name": "CC4 Estimate Summary Correction",
                    "Value": ""
                },
                {
                    "Name": "S Walk Around Done",
                    "Value": ""
                },
                {
                    "Name": "S1 All Expectations Set In Writing/Photos",
                    "Value": ""
                },
                {
                    "Name": "S2 I Feel Est. Will Close In",
                    "Value": ""
                },
                {
                    "Name": "S3 I went for",
                    "Value": ""
                },
                {
                    "Name": "E5 Movement Difficulty",
                    "Value": ""
                },
                {
                    "Name": "CC3 Time Lost ",
                    "Value": ""
                },
                {
                    "Name": "S4 Est Rating",
                    "Value": ""
                },
                {
                    "Name": "S6 Reason Client Chose our Company",
                    "Value": ""
                },
                {
                    "Name": "S5 Solutionist notes",
                    "Value": ""
                },
                {
                    "Name": "W1 Called in Rought",
                    "Value": ""
                },
                {
                    "Name": "W2 initial walk around completed",
                    "Value": ""
                },
                {
                    "Name": "W4 Safety Concerns/Description",
                    "Value": ""
                },
                {
                    "Name": "W9 Client Rating",
                    "Value": ""
                },
                {
                    "Name": "S7 Follow Up Notes",
                    "Value": ""
                },
                {
                    "Name": "CC5 Reasons for Area of Improvement",
                    "Value": ""
                },
                {
                    "Name": "CC2 Est. Scheduling Area of Improvement",
                    "Value": ""
                },
                {
                    "Name": "P1 Man Hours",
                    "Value": ""
                }
            ],
            "ExternalSystemId": null
        }
    ],
    "Success": true,
    "Error": null
}

enter image description here

答案

这里是VBA示例,显示了如何解析该数据。 JSON.basjsonExt.bas模块从VBA JSON parser导入到VBA项目中以进行JSON处理,并包括对“ Microsoft Scripting Runtime”的引用

以上是关于以特定方式在Excel中解析JSON嵌套数组的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Ios 中解析数组数据中的嵌套 Json 对象

使用 python/pandas 从特定文件夹中读取几个嵌套的 .json 文件到 excel 中

在 Excel VBA 中解析 JSON

在 IOS 中解析嵌套的 JSON 代码

解析嵌套的 JSONArray 并以 Recyclerview 正确的方式显示它

Mantle 2.0 无法为嵌套数组中的符号解析 JSON