【发布时间】:2021-05-31 14:47:57
【问题描述】:
我需要一些帮助来读取 vs2019 vb.net 中的 json 文件。我已经弄清楚了大部分内容,但是有一个子级别我不知道如何编写代码来阅读它。我正在使用 Newtonsoft.json 参考库。
这里是 json 数据的示例:
[
{
"checkDate": "2020-02-07",
"periodBeginDate": "2020-01-27",
"periodEndDate": "2020-02-02",
"earnings": [
{
"earningCode": "REG",
"hours": 0.0000,
"rate": 0.000000,
"amount": 55.00,
"chargeDate": null,
"jobCode": "",
"in1": null,
"out1": null,
"in2": null,
"out2": null,
"memo": "",
"internalMemo": "",
"costCenters": [
{
"level": 1,
"levelCode": "DEPT",
"costCenterCode": "Gradall operator"
}
]
}
],
"costCenters": [
{
"level": 1,
"levelCode": "DEPT",
"costCenterCode": "Gradall operator"
}
],
"divisionKey": {
"companyCode": "IT1",
"divisionCode": "001"
},
"employeeNumber": "999"
}
]
如您所见,可能有多个成本中心,一个位于根级别,一个位于收入部分。我需要两者兼得。我可以使用下面的代码获得根级别(和其他数据)。但我无法弄清楚如何在“收益”下为“成本中心”编码。我需要一个全新的课程吗?它是否属于“收益”?我是阅读 json 文件的新手,因此将不胜感激。
Public Class CheckData
Public Property EmployeeNumber As String = ""
Public Property DivisionKey As divisionKeys
Public Property Earnings() As List(Of Earning)
Public Property CostCenters() As List(Of CostCenter)
<JsonProperty("periodEndDate")>
Public Property PPDEndDate As Date?
<JsonProperty("checkDate")>
Public Property CheckDate As Date?
Public Class divisionKeys
<JsonProperty("divisionKey")>
Public Property divisionKey() As Object
Public Property companyCode As String = ""
Public Property divisionCode As String = ""
End Class
Public Class CostCenter
<JsonProperty("costCenters")>
Public Property costCenters() As Object
Public Property startDate As Date
Public Property levelCode As String = ""
Public Property costCenterCode As String = ""
End Class
Public Class Earning
<JsonProperty("earnings")>
Public Property earnings1() As Object
Public Property earningCode As String = ""
Public Property hours As Decimal
Public Property amount As Decimal
End Class
Private Sub GetJsonData()
SecFileName = "PayrollData.json"
Dim jsonString = IO.File.ReadAllText(PathJson & SecFileName)
Dim AllEmpl = JsonConvert.DeserializeObject(Of List(Of CheckData))(jsonString)
Try
Using cn As New SqlConnection(ConnString)
cn.Open()
For Each Emp In AllEmpl
EarnCount = Emp.Earnings.Count
For i = 0 To EarnCount - 1
Using cmd As New SqlCommand("Insert Into tblFTEImportBase (EmpId, FacNumPay, JobCode, PaycheckDate, PPDEndDate, EarnType, EarnHours, EarnAmt)
Values (@EmpId, @FacNumPay, @JobCode, @CheckDate, @PPDEndDate, @EarnType, @EarnHours, @EarnAmt);", cn)
With cmd.Parameters
.Add("@EmpId", SqlDbType.VarChar, 15).Value = Emp.EmployeeNumber
.Add("@FacNumPay", SqlDbType.VarChar, 10).Value = Emp.DivisionKey.companyCode
For cc = 0 To Emp.CostCenters.Count - 1
Select Case Emp.CostCenters(cc).levelCode
Case "DEPT"
.Add("@JobCode", SqlDbType.VarChar, 15).Value = Emp.CostCenters(cc).costCenterCode
'Case "JC"
' .Add("@DeptNum", SqlDbType.VarChar, 15).Value = Emp.CostCenters(cc).costCenterCode
End Select
Next
If IsDBNull(Emp.CheckDate) = True Or Emp.CheckDate Is Nothing Then
.Add("@CheckDate", SqlDbType.Date).Value = DBNull.Value
Else
.Add("@CheckDate", SqlDbType.Date).Value = Emp.CheckDate
End If
If IsDBNull(Emp.PPDEndDate) = True Or Emp.PPDEndDate Is Nothing Then
.Add("@PPDEndDate", SqlDbType.Date).Value = DBNull.Value
Else
.Add("@PPDEndDate", SqlDbType.Date).Value = Emp.PPDEndDate
End If
EarnTypeTest = Emp.Earnings(0).earningCode
.Add("@EarnType", SqlDbType.VarChar, 10).Value = Emp.Earnings(i).earningCode
.Add("@EarnHours", SqlDbType.Decimal).Value = Emp.Earnings(i).hours
.Add("@EarnAmt", SqlDbType.Decimal).Value = Emp.Earnings(i).amount
cmd.ExecuteNonQuery()
End With
End Using
Next
Next
cn.Close()
End Using
Catch ex As Exception
SuccessFlag = "N"
Errormsg = ex.Message
End Try
End Sub
【问题讨论】: