反序列化期间将JSON数组视为字符串

时间:2018-09-03 12:03:18

标签: arrays json string vb.net

很抱歉发布了较长的帖子,尝试提供尽可能多的信息

我正在尝试使用从https://www.codeproject.com/Questions/1231595/Vb-net-deserialize-json获得的一些代码反序列化JSON字符串。

我到目前为止的代码如下

主要功能

Private Function ExtractDataFromJSONToDataRecord(StartDate As Date, EndDate As Date)
Dim FormattedStart As String = Format(StartDate, "yyyy-MM-dd") 'Format the date for the API
Dim FormattedEnd As String = Format(EndDate, "yyyy-MM-dd")
Dim URLStr As String = "TestJSON.txt" '& FormattedStart & "/" & FormattedEnd
Dim JSONStr As String = SimulateScraper(URLStr) 'Scrapes the JSON from the API url(at the moment just using a file for debugging)
Dim Data As New List(Of DataRecord)'DataRecord is a custom class defined below
Dim Fields As List(Of Object)

Fields = JToken.Parse(JSONStr).FindTokens(FieldNames(0)).Values(Of Object)().ToList() 'Add as many objects to the Data list as will be necessary
For Each item As Object In Fields
    Dim NewRecord As New DataRecord
    Data.Add(NewRecord) 'Data is a list of DataRecord(custom class defined below)
Next

For i = 0 To FieldNames.Count - 1

    Fields = JToken.Parse(JSONStr).FindTokens(FieldNames(i)).Values(Of Object)().ToList() 'Find all objects with the name given by FieldNames(i), which is a list of the fields to make it simpler to cycle through them
    For Each item As Object In Fields

        If i = 2 Or i = 14 Then 'For specific fields date format is used so convert that to a DateTime type to store in list
            Data(item).SetValueAtIndex(i, ConvertDateFromUnixEpochToDateTime(item))
        Else
            Data(item).SetValueAtIndex(i, item)
        End If
    Next
Next
Return Data
End Function

然后是JsonExtensions类

Public Module JsonExtensions
<Extension>
Public Function FindTokens(containerToken As JToken, name As String) As List(Of JToken)
    Dim matches = New List(Of JToken)()
    FindTokens(containerToken, name, matches)
    Return matches
End Function
Private Sub FindTokens(containerToken As JToken, name As String, matches As List(Of JToken))
    If containerToken.Type = JTokenType.[Object] Then'JTokenType returns String {8}
        For Each child As JProperty In containerToken.Children(Of JProperty)()
            If child.Name = name Then
                matches.Add(child.Value)
            End If
            FindTokens(child.Value, name, matches)
        Next
    ElseIf containerToken.Type = JTokenType.Array Then
        For Each child As JToken In containerToken.Children()
            FindTokens(child, name, matches)
        Next
    End If
End Sub

最后是DataRecord类

Public Property ACCOUNT_ID As Integer
Public Property ACCOUNT_NO As String
Public Property ADDED As Integer
Public Property ACCOUNT_NAME As String
Public Property ADDRESS1 As String
Public Property ADDRESS2 As String
Public Property ADDRESS3 As String
Public Property ADDRESS4 As String
Public Property ADDRESS5 As String
Public Property POSTCODE As String
Public Property TELEPHONE As String
Public Property MOBILE As String
Public Property EMAIL As String
Public Property UserName As String
Public Property CANCELLED As Integer
Public Property HEAR As String

Public Sub SetValueAtIndex(Index As Integer, Value As Object)

    Select Case Index
        Case 0
            ACCOUNT_ID = Value
        Case 1
            ACCOUNT_NO = Value
        Case 2
            ADDED = Value
        Case 3
            ACCOUNT_NAME = Value
        Case 4
            ADDRESS1 = Value
        Case 5
            ADDRESS2 = Value
        Case 6
            ADDRESS3 = Value
        Case 7
            ADDRESS4 = Value
        Case 8
            ADDRESS5 = Value
        Case 9
            POSTCODE = Value
        Case 11
            TELEPHONE = Value
        Case 12
            MOBILE = Value
        Case 13
            EMAIL = Value
        Case 14
            UserName = Value
        Case 15
            CANCELLED = Value
        Case 16
            HEAR = Value
    End Select

End Sub

我想发生的事情是将JSON对象移到字段列表中,然后在每次遍历字段时进行迭代,然后将一条记录添加到DataRecord列表中,这样我就可以轻松地获得一个列表可访问的对象,该对象存储了JSON字符串中的所有数据,因此我只能调用Data(0).ACCOUNT_ID等。

我遇到的问题是输入JSON就是这样

"[
    {
        \"ACCOUNT_ID\":12345,
        \"ACCOUNT_NO\":\"A0012345\",
        \"ADDED\":\"\\/Date(1514796540000)\\/\",
        \"ACCOUNT_NAME\":\"Mr John Smith\",
        \"ADDRESS1\":\"1 Example St\",
        \"ADDRESS2\":\"Address Line 2\",
        \"ADDRESS3\":\"Area\",
        \"ADDRESS4\":\"Town\",
        \"ADDRESS5\":\"County\",
        \"POSTCODE\":\"AB1 2CD\",
        \"TELEPHONE\":\"01234567890\",
        \"MOBILE\":\"07123456789\",
        \"EMAIL\":\"example@gmail.com\",
        \"UserName\":\"\",
        \"REP\":\"\",
        \"CANCELLED\":null,
        \"HEAR\":\"Google\"
    },
    {
        \"ACCOUNT_ID\":23456,
        \"ACCOUNT_NO\":\"A0023456\",
        \"ADDED\":\"\\/Date(1514796540000)\\/\",
        \"SSD\":\"\\/Date(1516579200000)\\/\",
        \"ACCOUNT_NAME\":\"Mr John Smith\",
        \"ADDRESS1\":\"1 Example St\",
        \"ADDRESS2\":\"Address Line 2\",
        \"ADDRESS3\":\"Area\",
        \"ADDRESS4\":\"Town\",
        \"ADDRESS5\":\"County\",
        \"POSTCODE\":\"AB1 2CD\",
        \"TELEPHONE\":\"01234567890\",
        \"MOBILE\":\"07123456789\",
        \"EMAIL\":\"example@gmail.com\",
        \"UserName\":\"\",
        \"CANCELLED\":null,
        \"HEAR\":\"Google\"
    }
]"

,但是一旦将其解析并传递给FindTokens函数,其格式就是

{
    [
        {
            ACCOUNT_ID:12345,
            ACCOUNT_NO:A0012345,
            ADDED:\/Date(1514796540000)\/,
            ACCOUNT_NAME:Mr John Smith,
            ADDRESS1:1 Example St,
            ADDRESS2:Address Line 2,
            ADDRESS3:Area,
            ADDRESS4:Town,
            ADDRESS5:County,
            POSTCODE:AB1 2CD,
            TELEPHONE:01234567890,
            MOBILE:07123456789,
            EMAIL:example@gmail.com,
            UserName:,
            REP:,
            CANCELLED:null,
            HEAR:Google
        },
        {
            ACCOUNT_ID:23456,
            ACCOUNT_NO:A0023456,
            ADDED:\/Date(1514796540000)\/,
            SSD:\/Date(1516579200000)\/,
            ACCOUNT_NAME:Mr John Smith,
            ADDRESS1:1 Example St,
            ADDRESS2:Address Line 2,
            ADDRESS3:Area,
            ADDRESS4:Town,
            ADDRESS5:County,
            POSTCODE:AB1 2CD,
            TELEPHONE:01234567890,
            MOBILE:07123456789,
            EMAIL:example@gmail.com,
            UserName:,
            CANCELLED:null,
            HEAR:Google
        }
    ]
}

这被视为一个字符串,这意味着它无法识别字段名称,因此返回一个空列表。 我不太确定自己在做什么错或如何修改它以接受数组而不是字符串。

任何答案都很感激=)

0 个答案:

没有答案