我得到了一些帮助,但不确定是应该在原始问题中继续还是开始新的问题,因为从技术上讲这是一个新问题(我选择了新问题,但请让我知道您下次喜欢什么)
我想出了如何将标准信息集解析为不同的列。我在解析嵌套数组时遇到麻烦。到达代码的那部分时,它会给出 运行时错误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:\Users\jb\VBVA Team Dropbox\Y&M \Owner Files\auto update vba macro\sb 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": "\nGUTTER - NO EXISTING GUTTERS\nClient 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\nLlyods 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\n\nHouse Color: Orange Brick \nDoes Customer Have Dogs: YES- small\n\nDate Estimate Scheduled & Email Sent:\n\nScheduling Instructions: \n\nDate Called & Emailed to Schedule:\nDate of Service Text & Email: \n\nBilling Instructions:\nDeposit $ \nBalance Due $ \n\nInstaller Instructions: \n\n\nInstaller Notes:\nPROS:\n\nCONS:\n\nWAS PAYMENT TAKEN?",
"InvoiceNotes": null,
"ReminderType": "None",
"ReminderValue": 0,
"ReminderMessage": "",
"EstimateLines": [
{
"Id": 30130758,
"ParentId": 30130933,
"Inventory": {
"Id": 1688230,
"SKU": "2X3POALU",
"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": "5KHHALU",
"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": "5KISMALU 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": "5KIBYMALU 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": "5ONE24 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": "GTRCL 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": "5KOBYMALU 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": "5KLECALU 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\ngutter 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-DSALU-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": "5KALUWHI",
"Name": "5\" Aluminum White K-Style Gutter",
"Type": "Product"
},
"Price": 5,
"Quantity": 190,
"Description": "KStyle Gutters are designed to carry rainwater from the roof to the ground, protecting a structure from water damage. The KStyle 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 20year warranty.",
"IsTaxable": false,
"Tax": null
},
{
"Id": 30130934,
"ParentId": 30130933,
"Inventory": {
"Id": 1688653,
"SKU": "5KRECALU 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": "2X3UCALU 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": "ONEGUARD 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
}
更新
我尝试了答案中提出的解决方案,但遇到了一个问题-我需要零件信息位于列中列出的估算行中,并重复关联项目,如估算编号和自定义字段。我附了一张照片,看起来像什么:
答案 0 :(得分:1)
这是VBA示例,显示了如何解析该数据。 将JSON.bas和jsonExt.bas模块从VBA JSON parser导入到VBA项目中以进行JSON处理,并包括对“ Microsoft Scripting Runtime”的引用(看看{{ 3}}如何导入模块并添加引用。
Option Explicit
Sub test()
' retrieve json
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://pastebin.com/raw/wVtq027R", True
.Send
Do Until .ReadyState = 4: DoEvents: Loop
Dim resp As String
resp = .ResponseText
End With
Dim content
Dim state As String
JSON.Parse resp, content, state
Dim data
Dim exists
jsonExt.selectElement content, ".Data", data, exists
If Not exists Then MsgBox "No data": End
' convert CustomFields to conventional format {"propertyName":"propertyValue", ...}
Dim fieldNames As Dictionary
Set fieldNames = New Dictionary
Dim i
For i = 0 To UBound(data)
Dim custFields As Dictionary
Set custFields = New Dictionary
Dim custField
For Each custField In data(i)("CustomFields")
If custField("Value") <> "" Then
custFields(custField("Name")) = custField("Value")
fieldNames("CustomFields." & custField("Name")) = ""
End If
Next
Set data(i)("CustomFields") = custFields
Next
' specify fields neccessary to display
Dim head()
head = Array("WorkOrderNumber", "EstimateNumber", "EstimateDate", "WonOrLostDate", "ScheduledTime", "EstimatedDuration", "Customer.Name", "Conact.Name", "Location.Name", "GeoCoordinates.Latitude", "GeoCoordinates.Longitude", "MarketingCampaign.Name", "Team.name", "WorkOrderDate", "DateFinished", "ScheduledTime", "EstimatedDuration", "Notes", "PrivateNotes", "SalesRepresentative.Name", "Description", "Status", "IsInvoiced", "Metadata.CreatedBy", "Metadata.CreatedOn", "Metadata.UpdatedOn", "Metadata.UpdatedBy", "Metadata.Version")
jsonExt.pushItem head, fieldNames.keys(), , False
' convert json to arrays
Dim body()
jsonExt.toArray data, body, head, True
' output arrays to worksheet
With ThisWorkbook.Sheets(1)
.Activate
.Cells.Delete
With .Cells(1, 1)
.Resize(1, UBound(head) - LBound(head) + 1).Value = head
.Offset(1, 0).Resize( _
UBound(body, 1) - LBound(body, 1) + 1, _
UBound(body, 2) - LBound(body, 2) + 1 _
).Value = body
End With
.Columns.AutoFit
End With
MsgBox "Completed"
End Sub
对我来说输出如下: