【vb.net】轻量JSON序列及反序列化

这篇具有很好参考价值的文章主要介绍了【vb.net】轻量JSON序列及反序列化。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

这个代码写的有点时间了,可能有点小bug,欢迎评论区反馈

作用是将Json文本转化成一个HarryNode类进行相关的Json对象处理或者读取,也可以将一个HarryNode对象用ToString变为Json文本。

举例:

1、读取节点数据

dim harryNode = New HarryNode("", "{""msg"":""hello world!""}")
msgbox(harryNode.GetAtt("msg")) '弹窗显示hello world!


'下面展示更复杂的嵌套读取
dim harryNode = New HarryNode("", "{""node1"": {""msg"":""hello world!""}}")
msgbox(harryNode.GetAtt("node1.msg")) '弹窗显示hello world! 没错,用“.”作为路径连接符进行寻址

'如果json的键里包含“.”可以将源码里的“.”替换成其它字符,也可以这样进行取值
msgbox(harryNode.GetAtt("node1")("msg").value
'这里的harryNode.GetAtt("node1")返回的是一个字典对象(String, HarryNode)

2、创建新Json节点,写入数据并输出文本

Dim harryNode = New HarryNode("", "{}")
harryNode.SetAtt("msg", """hello world!""")
MsgBox(harryNode.ToString)

'可以看到SetAtt方法的第二个参数输入的字符串需要是json字符串格式,因此字符串本身需要加双引号
'下面可以用SetAtt的另一种重载方法,与上面代码的结果相同
harryNode.SetAtt("msg", "hello world!", NodeTypeEnum.isString)
MsgBox(harryNode.ToString)

'同样,对嵌套的复杂json对象,可以如下
harryNode.SetAtt("node1.msg", "hello world!", NodeTypeEnum.isString)
'下面这样写也是可以的
harryNode.SetAtt("node1", "{""msg"": ""hello world!""}")

文档

1、方法和函数

New

构造函数

name String 节点的名字(对于根节点此项没啥意义)
json String 要解析构造的JSON串
parent HarryNode 实例的父节点
name String 节点的名字(对于根节点此项没啥意义)
nodeValue Object 节点VB.NET对象值
type NodeTypeEnum 节点值的类型
parent HarryNode 实例的父节点

GetAtt

获得指定路径的VB.NET对象

path String 节点路径
defaultValue Object 没有获取到返回的值,默认Nothing

SetAtt

根据指定路径设置节点值

path String 节点路径
newValue Object 节点的值(VB.NET对象)
newValueType NodeTypeEnum 值的类型
path String 节点路径
json String 节点的值(JSON字符串)

ReName

重命名某个节点

path String 节点路径
newName String 新名字

ToJson

返回JSON字符串,与ToString()等价

GetNode

获得指定路径的HarryNode对象

path String 节点路径

AddNode

添加子节点

path String 节点路径
nodeName String 子节点名
nodeJson String 子节点JSON串

Del

删除指定路径的节点

path String 节点路径

Merge

合并两个字典节点;

node HarryNode 要合并的节点

GetChildPath

返回一个当前节点子节点名的列表

Add

指定某个节点的数据加一个值

path String 节点路径
addValue Single 加数

ConAdd

指定某个节点的数据加一个值,但是限制了数的范围

path String 节点路径
addValue Single 加数
maxValue Single 最大值
minValue Single 最小值,默认0

Mul

指定某个节点的数据乘一个值

path String 节点路径
addValue Single 乘数

Power

指定某个节点的数据求次幂

path String 节点路径
addValue Single

2、属性

Value

当前节点的VB.NET类型值

3、事件

NodeContentChangeBefore

节点内容改变之前

path String 节点路径
newValue Object 即将变成的值
newValueType NodeTypeEnum 即将变成值的类型

NodeContentChangeBeforeFromJson

节点内容改变之前(通过JSON解释)

path String 节点路径
json String 即将变成的值的JSON字符串

NodeContentChangeLater

节点内容改变之后

path String 节点路径
newValue Object 变成的值
newValueType NodeTypeEnum 变成值的类型

NodeContentChangeLaterFromJson

节点内容改变之后(通过JSON解释)文章来源地址https://www.toymoban.com/news/detail-724254.html

path String 节点路径
json String 变成的值的JSON字符串

源码如下:

Imports System.Text.RegularExpressions
Public Class HarryNode
    Public Shared pathSeparator As String = "."
    Public Shared outputFormat As Boolean = True
    Public Shared formatRetraction As Integer = 2
    Public Shared Function MulReplace(source As String, ParamArray args() As String) As String
        If args.Length Mod 2 <> 0 Then
            Return source
        End If
        For i As Integer = 0 To UBound(args) Step 2
            source = Replace(source, args(i), args(i + 1))
        Next
        Return source
    End Function
    Public Shared Function ToEscape(source As String) As String
        Return MulReplace(source, "\", "\\", vbCrLf, "\n", vbTab, "\t", """", "\""", Chr(8), "\b", Chr(12), "\f")
    End Function
    Public Enum NodeTypeEnum
        isNull = 0
        isString = 1
        isSingle = 2
        isDict = 3
        isList = 4
        isBool = 5
    End Enum
    Public nodeType As NodeTypeEnum
    Public nodeName As String
    Public parentNode As HarryNode

    Private stringValue As String
    Private singleValue As Single
    Private boolValue As Boolean
    Private childNode As Dictionary(Of String, HarryNode)

    Public Event NodeContentChangeBefore(ByRef path As String, ByRef newValue As Object, ByRef newValueType As String)
    Public Event NodeContentChangeBeforeFromJson(ByRef path As String, ByRef json As String)

    Public Event NodeContentChangeLater(path As String, ByRef nowValue As Object, ByRef newValueType As NodeTypeEnum)
    Public Event NodeContentChangeLaterFromJson(path As String, nowJson As String)
    Public Sub Merge(node As HarryNode)
        If nodeType = node.nodeType And nodeType = NodeTypeEnum.isDict Then
            For i = 0 To node.childNode.Count - 1
                Dim key = node.childNode.Keys(i)
                If childNode.ContainsKey(key) Then
                    childNode(key).Merge(node.childNode(key))
                Else
                    childNode.Add(key, node.childNode(key))
                End If
            Next
        End If
    End Sub
    Public Function GetChildPath() As List(Of String)
        Dim result As New List(Of String)
        If nodeType = NodeTypeEnum.isDict Or nodeType = NodeTypeEnum.isList Then
            result.AddRange(childNode.Keys)
        Else
            result.Add(nodeName)
        End If
        Return result
    End Function
    'Public Function GetTreeNode(interpreter As 解释器) As TreeNode
    '    Dim rootNode As New TreeNode(nodeName & interpreter.Search(nodeName))
    '    If nodeType = NodeTypeEnum.isDict Or nodeType = NodeTypeEnum.isList Then
    '        For Each cNode In childNode
    '            rootNode.Nodes.Add(cNode.Value.GetTreeNode(interpreter))
    '        Next
    '    Else
    '        rootNode.Nodes.Add(Value & interpreter.Search(Value))
    '    End If
    '    Return rootNode
    'End Function
    Public Sub Power(path As String, addValue As Single)
        SetAtt(path, GetAtt(path, 0) ^ addValue, 0)
    End Sub
    Public Sub Add(path As String, addValue As Single)
        SetAtt(path, GetAtt(path, 0) + addValue, 0)
    End Sub
    Public Sub ConAdd(path As String, addValue As Single, maxValue As Single, Optional minValue As Single = 0)
        Dim newValue As Single = GetAtt(path, 0) + addValue
        If newValue > maxValue Then
            newValue = maxValue
        End If
        If newValue < minValue Then
            newValue = minValue
        End If
        SetAtt(path, newValue, 0)
    End Sub
    Public Sub Mul(path As String, addValue As Single)
        SetAtt(path, GetAtt(path, 0) * addValue, 0)
    End Sub
    Public Sub AddNode(path As String, nodeName As String, nodeJson As String)
        Dim paths() As String = path.Split(pathSeparator)
        Dim p As String
        Dim node As HarryNode = Me
        For i As Integer = 0 To UBound(paths) - 1
            p = paths(i)
            Select Case node.nodeType
                Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                    If node.nodeType = NodeTypeEnum.isList Then
                        p = Int(Val(p))
                    End If
                    If Not node.childNode.ContainsKey(p) Then
                        node.childNode.Add(p, New HarryNode(p, "{}", Me))
                    End If
                Case Else
                    node.nodeType = NodeTypeEnum.isDict
                    node.childNode = New Dictionary(Of String, HarryNode)
                    If Not node.childNode.ContainsKey(p) Then
                        node.childNode.Add(p, New HarryNode(p, "{}", Me))
                    End If
            End Select
            node = node.childNode(p)
        Next
        p = paths.Last
        Select Case node.nodeType
            Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                If node.nodeType = NodeTypeEnum.isList Then
                    p = Int(Val(p))
                End If
                If Not node.childNode.ContainsKey(p) Then
                    node.childNode.Add(p, New HarryNode(p, "{}", Me))
                End If
            Case Else
                node.nodeType = NodeTypeEnum.isDict
                node.childNode = New Dictionary(Of String, HarryNode)
                If Not node.childNode.ContainsKey(p) Then
                    node.childNode.Add(p, New HarryNode(p, "{}", Me))
                End If
        End Select
        If Not node.childNode.ContainsKey(p) Then
            node.childNode.Add(p, New HarryNode(nodeName, nodeJson, Me))
        Else
            node.childNode(p) = New HarryNode(nodeName, nodeJson, Me)
        End If
    End Sub
    Public Sub Del(path As String)
        Dim paths() As String = path.Split(pathSeparator)
        Dim p As String
        Dim node As HarryNode = Me
        For i As Integer = 0 To UBound(paths) - 1
            p = paths(i)
            Select Case node.nodeType
                Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                    If node.nodeType = NodeTypeEnum.isList Then
                        p = Int(Val(p))
                    End If
                    If Not node.childNode.ContainsKey(p) Then
                        Return
                    End If
                Case Else
                    node.nodeType = NodeTypeEnum.isDict
                    node.childNode = New Dictionary(Of String, HarryNode)
                    If Not node.childNode.ContainsKey(p) Then
                        Return
                    End If
            End Select
            node = node.childNode(p)
        Next
        p = paths.Last
        Select Case node.nodeType
            Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                If node.nodeType = NodeTypeEnum.isList Then
                    p = Int(Val(p))
                End If
                If Not node.childNode.ContainsKey(p) Then
                    Return
                End If
            Case Else
                node.nodeType = NodeTypeEnum.isDict
                node.childNode = New Dictionary(Of String, HarryNode)
                If Not node.childNode.ContainsKey(p) Then
                    Return
                End If
        End Select
        node.childNode.Remove(p)
    End Sub
    Public Function GetAtt(path As String, Optional defaultValue As Object = Nothing) As Object
        If path = "" Then
            Return Value
        End If
        Dim paths() As String = path.Split(pathSeparator)
        Dim node As HarryNode = Me
        For Each p As String In paths
            Select Case node.nodeType
                Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                    If node.childNode.ContainsKey(p) Then
                        node = node.childNode(p)
                    Else
                        Return defaultValue
                    End If
                Case Else
                    Return defaultValue
            End Select
        Next
        Return node.Value
    End Function
    Public Function GetNode(path As String) As HarryNode
        If path = "" Then
            Return Me
        End If
        Dim p As String
        Dim paths() As String = path.Split(pathSeparator)
        Dim node As HarryNode = Me
        For i As Integer = 0 To UBound(paths) - 1
            p = paths(i)
            Select Case node.nodeType
                Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                    If node.childNode.ContainsKey(p) Then
                        node = node.childNode(p)
                    Else
                        Return New HarryNode("", "", Me)
                    End If
                Case Else
                    Return New HarryNode("", "", Me)
            End Select
        Next
        If node.childNode IsNot Nothing AndAlso node.childNode.ContainsKey(paths.Last) Then
            Return node.childNode(paths.Last)
        End If
        Return New HarryNode(paths.Last, String.Format("""{0}""", paths.Last), Me)
    End Function
    Public Sub SetAtt(path As String, newValue As Object, newValueType As String)
        RaiseEvent NodeContentChangeBefore(path, newValue, newValueType)
        Dim paths() As String = path.Split(pathSeparator)
        Dim p As String
        Dim node As HarryNode = Me
        For i As Integer = 0 To UBound(paths) - 1
            p = paths(i)
            Select Case node.nodeType
                Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                    If node.nodeType = NodeTypeEnum.isList Then
                        p = Int(Val(p))
                    End If
                    If Not node.childNode.ContainsKey(p) Then
                        node.childNode.Add(p, New HarryNode(p, "{}", Me))
                    End If
                Case Else
                    node.nodeType = NodeTypeEnum.isDict
                    node.childNode = New Dictionary(Of String, HarryNode)
                    If Not node.childNode.ContainsKey(p) Then
                        node.childNode.Add(p, New HarryNode(p, "{}", Me))
                    End If
            End Select
            node = node.childNode(p)
        Next
        p = paths.Last
        Select Case node.nodeType
            Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                If node.nodeType = NodeTypeEnum.isList Then
                    p = Int(Val(p))
                End If
                If Not node.childNode.ContainsKey(p) Then
                    node.childNode.Add(p, New HarryNode(p, newValue, newValueType, Me))
                End If
            Case Else
                node.nodeType = NodeTypeEnum.isDict
                node.childNode = New Dictionary(Of String, HarryNode)
                If Not node.childNode.ContainsKey(p) Then
                    node.childNode.Add(p, New HarryNode(p, newValue, newValueType, Me))
                End If
        End Select
        node.childNode(p).Value = newValue
        RaiseEvent NodeContentChangeLater(path, node.childNode(p).Value, node.nodeType)
    End Sub
    Public Sub ReName(path As String, newName As String)
        Dim paths() As String = path.Split(pathSeparator)
        Dim p As String
        Dim node As HarryNode = Me
        For i As Integer = 0 To UBound(paths) - 1
            p = paths(i)
            Select Case node.nodeType
                Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                    If node.nodeType = NodeTypeEnum.isList Then
                        p = Int(Val(p))
                    End If
                    If Not node.childNode.ContainsKey(p) Then
                        node.childNode.Add(p, New HarryNode(p, "{}", Me))
                    End If
                Case Else
                    node.nodeType = NodeTypeEnum.isDict
                    node.childNode = New Dictionary(Of String, HarryNode)
                    If Not node.childNode.ContainsKey(p) Then
                        node.childNode.Add(p, New HarryNode(p, "{}", Me))
                    End If
            End Select
            node = node.childNode(p)
        Next
        p = paths.Last
        Select Case node.nodeType
            Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                If node.nodeType = NodeTypeEnum.isList Then
                    p = Int(Val(p))
                End If
                If node.childNode.ContainsKey(p) Then
                    ' 修改
                    node.childNode.Add(newName, New HarryNode(newName, node.childNode(p).ToJson, Me))
                    node.childNode.Remove(p)
                End If
            Case Else
                node.nodeType = NodeTypeEnum.isDict
                node.childNode = New Dictionary(Of String, HarryNode)
                If node.childNode.ContainsKey(p) Then
                    node.childNode.Add(newName, New HarryNode(newName, node.childNode(p).ToJson, Me))
                    node.childNode.Remove(p)
                End If
        End Select
    End Sub
    Public Sub SetAtt(path As String, json As String)
        RaiseEvent NodeContentChangeBeforeFromJson(path, json)
        Dim paths() As String = path.Split(pathSeparator)
        Dim p As String
        Dim node As HarryNode = Me
        For i As Integer = 0 To UBound(paths) - 1
            p = paths(i)
            Select Case node.nodeType
                Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                    If node.nodeType = NodeTypeEnum.isList Then
                        p = Int(Val(p))
                    End If
                    If Not node.childNode.ContainsKey(p) Then
                        node.childNode.Add(p, New HarryNode(p, "{}", Me))
                    End If
                Case Else
                    node.nodeType = NodeTypeEnum.isDict
                    node.childNode = New Dictionary(Of String, HarryNode)
                    If Not node.childNode.ContainsKey(p) Then
                        node.childNode.Add(p, New HarryNode(p, "{}", Me))
                    End If
            End Select
            node = node.childNode(p)
        Next
        p = paths.Last
        Select Case node.nodeType
            Case NodeTypeEnum.isDict, NodeTypeEnum.isList
                If node.nodeType = NodeTypeEnum.isList Then
                    p = Int(Val(p))
                End If
                If Not node.childNode.ContainsKey(p) Then
                    node.childNode.Add(p, New HarryNode(p, "{}", Me))
                End If
            Case Else
                node.nodeType = NodeTypeEnum.isDict
                node.childNode = New Dictionary(Of String, HarryNode)
                If Not node.childNode.ContainsKey(p) Then
                    node.childNode.Add(p, New HarryNode(p, "{}", Me))
                End If
        End Select
        node.childNode(p).JsonToValue(json)
        RaiseEvent NodeContentChangeLaterFromJson(path, json)
    End Sub
    Public Function ToJson(Optional deep As Integer = 1) As String
        If outputFormat Then
            Dim deepFormatRetraction = New String(" ", deep * formatRetraction)
            Dim deepFormatRetractionSub1 = New String(" ", (deep - 1) * formatRetraction)
            Select Case nodeType
                Case NodeTypeEnum.isString
                    Return String.Format("""{0}""", ToEscape(stringValue))
                Case NodeTypeEnum.isBool
                    Return boolValue.ToString.ToLower
                Case NodeTypeEnum.isSingle
                    Return singleValue
                Case NodeTypeEnum.isDict
                    Dim result As New List(Of String)
                    For i As Integer = 0 To childNode.Count - 1
                        result.Add(String.Format(deepFormatRetraction & """{0}"": {1}", childNode.Keys(i), childNode.Values(i).ToJson(deep + 1)))
                    Next
                    Return String.Format(Replace("{{\n{0}\n{1}}}", "\n", vbCrLf), Join(result.ToArray, "," & vbCrLf), deepFormatRetractionSub1)
                Case NodeTypeEnum.isList
                    Dim result As New List(Of String)
                    For i As Integer = 0 To childNode.Count - 1
                        result.Add(deepFormatRetraction & childNode.Values(i).ToJson(deep + 1))
                    Next
                    Return String.Format(Replace("[\n{0}\n{1}]", "\n", vbCrLf), Join(result.ToArray, "," & vbCrLf), deepFormatRetractionSub1)
                Case Else
                    Return ""
            End Select
        End If
        Select Case nodeType
            Case NodeTypeEnum.isString
                Return String.Format("""{0}""", ToEscape(stringValue))
            Case NodeTypeEnum.isBool
                Return boolValue
            Case NodeTypeEnum.isSingle
                Return singleValue
            Case NodeTypeEnum.isDict
                Dim result As New List(Of String)
                For i As Integer = 0 To childNode.Count - 1
                    result.Add(String.Format("""{0}"":{1}", childNode.Keys(i), childNode.Values(i).ToJson))
                Next
                Return String.Format("{{{0}}}", Join(result.ToArray, ","))
            Case NodeTypeEnum.isList
                Dim result As New List(Of String)
                For i As Integer = 0 To childNode.Count - 1
                    result.Add(childNode.Values(i).ToJson)
                Next
                Return String.Format("[{0}]", Join(result.ToArray, ","))
            Case Else
                Return ""
        End Select
    End Function
    Public Overloads Function ToString() As String
        Return ToJson()
    End Function
    Public Property Value() As Object
        Get
            Select Case nodeType
                Case NodeTypeEnum.isString
                    Return stringValue
                Case NodeTypeEnum.isBool
                    Return boolValue
                Case NodeTypeEnum.isSingle
                    Return singleValue
                Case NodeTypeEnum.isDict
                    Return childNode
                Case NodeTypeEnum.isList
                    Return childNode.Values
                Case Else
                    Return Nothing
            End Select
        End Get
        Set(value As Object)
            Select Case nodeType
                Case NodeTypeEnum.isString
                    stringValue = value
                Case NodeTypeEnum.isBool
                    boolValue = value
                Case NodeTypeEnum.isSingle
                    singleValue = value
                Case NodeTypeEnum.isDict
                    childNode = value
                Case NodeTypeEnum.isList
                    Dim valueList As List(Of HarryNode) = value
                    childNode.Clear()
                    For i As Integer = 0 To valueList.Count - 1
                        childNode.Add(i, valueList(i))
                    Next
            End Select
        End Set
    End Property
    Public Sub JsonToValue(json As String)
        If json Is Nothing Then
            Return
        End If
        json = Regex.Match(json, "^\s*(.*?)\s*$", RegexOptions.Singleline).Groups(1).Value
        If Regex.IsMatch(json, "^"".*""$", RegexOptions.Singleline) Then
            '字符串
            nodeType = NodeTypeEnum.isString
            stringValue = json.Substring(1, json.Length - 2)
        ElseIf Regex.IsMatch(json, "^{.*}$", RegexOptions.Singleline) Then
            '字典
            nodeType = NodeTypeEnum.isDict
            If json = "{}" OrElse Regex.IsMatch(json, "^\s*\{\s*\}\s*$") Then
                childNode = New Dictionary(Of String, HarryNode)
            Else
                childNode = GetDict(json, Me)
            End If
        ElseIf Regex.IsMatch(json, "^\[.*\]$", RegexOptions.Singleline) Then
            '列表
            nodeType = NodeTypeEnum.isList
            If json = "[]" OrElse Regex.IsMatch(json, "^\s*\[\s*\]\s*$") Then
                childNode = New Dictionary(Of String, HarryNode)
            Else
                childNode = GetList(json, Me)
            End If
        ElseIf Regex.IsMatch(json, "^[-]{0,1}[\d]*[\.]{0,1}[\d]*$", RegexOptions.Singleline) Then
            '数值
            nodeType = NodeTypeEnum.isSingle
            singleValue = Val(json)
        Else
            '布尔值
            nodeType = NodeTypeEnum.isBool
            boolValue = GetBool(json)
        End If
    End Sub
    Public Shared Function GetDict(json As String, sourceNode As HarryNode) As Dictionary(Of String, HarryNode)
        'Debug.WriteLine(String.Format("GetDict.json={0}", json))
        Dim node As New Dictionary(Of String, HarryNode)
        Dim name As String = ""
        Dim temp As New List(Of String)
        Dim bigBrackets As Integer
        Dim colon As Integer
        Dim doubleQuotationMark As Integer
        Dim brackets As Integer
        Dim escape As Integer
        Dim stringContent As String
        Dim exegesis As Integer
        For Each c As String In json
            'Debug.WriteLine(Join(temp.ToArray, ""))
            'Debug.WriteLine("doubleQuotationMark={0}", doubleQuotationMark)
            'Debug.WriteLine("exegesis={0}", exegesis)
            'Debug.WriteLine("bigBrackets={0}", bigBrackets)
            'Debug.WriteLine("brackets={0}", brackets)
            'Debug.WriteLine("")
            If c = "/" Then
                exegesis += 1
                Continue For
            ElseIf exegesis = 1 Then
                temp.Add("/")
                exegesis = 0
            End If
            If exegesis >= 2 Then
                If c = vbCr Or c = vbLf Then
                    exegesis = 0
                Else
                    Continue For
                End If
            End If
            If doubleQuotationMark = 0 Then
                '未在字符串内时
                Select Case c
                    Case "{"
                        bigBrackets += 1
                        If bigBrackets > 1 OrElse brackets > 0 Then
                            '子嵌套记忆
                            temp.Add(c)
                        End If
                    Case "}"
                        bigBrackets -= 1
                        If bigBrackets > 1 OrElse brackets > 0 OrElse (bigBrackets = 1 AndAlso brackets = 0) Then
                            temp.Add(c)
                        End If
                    Case "["
                        brackets += 1
                        temp.Add(c)
                    Case "]"
                        brackets -= 1
                        temp.Add(c)
                    Case ":"
                        If bigBrackets = 1 AndAlso brackets = 0 Then
                            '第一层嵌套内
                            colon += 1
                        ElseIf bigBrackets > 1 OrElse brackets > 0 Then
                            temp.Add(c)
                        End If
                    Case """"
                        If bigBrackets = 1 AndAlso brackets = 0 Then
                            '第一层嵌套内
                            doubleQuotationMark += 1
                            temp.Add(c)
                        ElseIf bigBrackets > 1 OrElse brackets > 0 Then
                            temp.Add(c)
                        End If
                    Case ","
                        If colon > 0 AndAlso bigBrackets = 1 AndAlso brackets = 0 Then
                            '非字符串
                            If temp.Count > 0 Then
                                stringContent = Join(temp.ToArray, "")
                                temp.Clear()
                                node.Add(name, New HarryNode(name, stringContent, sourceNode))
                            Else
                                'null
                                node.Add(name, New HarryNode(name, Nothing, sourceNode))
                            End If
                            colon = 0
                        Else
                            temp.Add(c)
                        End If
                    Case Else
                        If bigBrackets > 1 Or Regex.IsMatch(c, "\S", RegexOptions.Singleline) Then
                            temp.Add(c)
                        End If
                End Select
            ElseIf bigBrackets = 1 AndAlso brackets = 0 Then
                '第一层嵌套内
                '在字符串内
                Select Case c
                    Case """"
                        temp.Add(c)
                        If escape = 1 Then
                            '转义"
                            escape = 0
                        Else
                            doubleQuotationMark = 0
                            If colon = 0 Then
                                '节点名
                                stringContent = Join(temp.ToArray, "")
                                temp.Clear()
                                name = stringContent.Substring(1, stringContent.Length - 2)
                            End If
                        End If
                    Case "\"
                        escape += 1
                        If escape > 1 Then
                            '转义\
                            temp.Add(c)
                            escape = 0
                        End If
                    Case "n"
                        If escape = 1 Then
                            '转义换行
                            temp.Add(vbCrLf)
                            escape = 0
                        Else
                            temp.Add(c)
                        End If
                    Case "b"
                        If escape = 1 Then
                            temp.Add(Chr(8))
                            escape = 0
                        Else
                            temp.Add(c)
                        End If
                    Case "f"
                        If escape = 1 Then
                            temp.Add(Chr(12))
                            escape = 0
                        Else
                            temp.Add(c)
                        End If
                    Case "t"
                        If escape = 1 Then
                            temp.Add(vbTab)
                            escape = 0
                        Else
                            temp.Add(c)
                        End If
                    Case Else
                        escape = 0
                        temp.Add(c)
                End Select
            End If
        Next
        If temp.Count > 0 Then
            stringContent = Join(temp.ToArray, "")
            temp.Clear()
            node.Add(name, New HarryNode(name, stringContent, sourceNode))
        Else
            'null
            node.Add(name, New HarryNode(name, Nothing, sourceNode))
        End If
        Return node
    End Function
    Public Shared Function GetList(json As String, sourceNode As HarryNode) As Dictionary(Of String, HarryNode)
        'Debug.WriteLine(String.Format("GetList.json={0}", json))
        Dim node As New Dictionary(Of String, HarryNode)
        Dim name As String
        Dim temp As New List(Of String)
        Dim bigBrackets As Integer
        Dim doubleQuotationMark As Integer
        Dim brackets As Integer
        Dim escape As Integer
        Dim comma As Integer
        Dim stringContent As String
        For Each c As String In json
            Dim exegesis As Integer
            If c = "/" Then
                exegesis += 1
                Continue For
            ElseIf exegesis = 1 Then
                temp.Add("/")
                exegesis = 0
            End If
            If exegesis >= 2 Then
                If c = vbCr Or c = vbLf Then
                    exegesis = 0
                Else
                    Continue For
                End If
            End If
            If doubleQuotationMark = 0 Then
                '未在字符串内时
                Select Case c
                    Case "["
                        brackets += 1
                        If brackets > 1 OrElse bigBrackets > 0 Then
                            '子嵌套记忆
                            temp.Add(c)
                        End If
                    Case "]"
                        brackets -= 1
                        If brackets > 1 OrElse bigBrackets > 0 OrElse (brackets = 1 AndAlso bigBrackets = 0) Then
                            temp.Add(c)
                        End If
                    Case "{"
                        bigBrackets += 1
                        temp.Add(c)
                    Case "}"
                        bigBrackets -= 1
                        temp.Add(c)
                    Case """"
                        If brackets = 1 AndAlso bigBrackets = 0 Then
                            '第一层嵌套内
                            doubleQuotationMark += 1
                            temp.Add(c)
                        ElseIf brackets > 1 OrElse bigBrackets > 0 Then
                            temp.Add(c)
                        End If
                    Case ","
                        If bigBrackets = 0 AndAlso brackets = 1 Then
                            name = comma
                            comma += 1
                            If temp.Count > 0 Then
                                stringContent = Join(temp.ToArray, "")
                                temp.Clear()
                                node.Add(name, New HarryNode(name, stringContent, sourceNode))
                            Else
                                'null
                                node.Add(name, New HarryNode(name, Nothing, sourceNode))
                            End If
                        Else
                            temp.Add(c)
                        End If
                    Case Else
                        If bigBrackets > 1 Or Regex.IsMatch(c, "\S", RegexOptions.Singleline) Then
                            temp.Add(c)
                        End If
                End Select
            ElseIf brackets = 1 AndAlso bigBrackets = 0 Then
                '第一层嵌套内
                '在字符串内
                Select Case c
                    Case """"
                        temp.Add(c)
                        If escape = 1 Then
                            '转义"
                            escape = 0
                        Else
                            doubleQuotationMark = 0
                        End If
                    Case "\"
                        escape += 1
                        If escape > 1 Then
                            '转义\
                            temp.Add(c)
                            escape = 0
                        End If
                    Case "n"
                        If escape = 1 Then
                            '转义换行
                            temp.Add(vbCrLf)
                            escape = 0
                        Else
                            temp.Add(c)
                        End If
                    Case "b"
                        If escape = 1 Then
                            temp.Add(Chr(8))
                            escape = 0
                        Else
                            temp.Add(c)
                        End If
                    Case "f"
                        If escape = 1 Then
                            temp.Add(Chr(12))
                            escape = 0
                        Else
                            temp.Add(c)
                        End If
                    Case "t"
                        If escape = 1 Then
                            temp.Add(vbTab)
                            escape = 0
                        Else
                            temp.Add(c)
                        End If
                    Case Else
                        escape = 0
                        temp.Add(c)
                End Select
            End If
        Next
        name = comma
        If temp.Count > 0 Then
            '非字符串
            stringContent = Join(temp.ToArray, "")
            temp.Clear()
            node.Add(name, New HarryNode(name, stringContent, sourceNode))
        Else
            'null
            node.Add(name, New HarryNode(name, Nothing, sourceNode))
        End If
        Return node
    End Function
    Public Shared Function GetBool(value As String) As Boolean
        If value.ToLower = "false" OrElse value = "0" Then
            Return False
        End If
        Return True
    End Function
    Public Sub New(name As String, json As String, Optional parent As HarryNode = Nothing)
        nodeName = name
        parentNode = parent
        JsonToValue(json)
    End Sub
    Public Sub New(name As String, nodeValue As Object, type As NodeTypeEnum, Optional parent As HarryNode = Nothing)
        nodeName = name
        nodeType = type
        parentNode = parent
        Value = nodeValue
    End Sub
End Class

到了这里,关于【vb.net】轻量JSON序列及反序列化的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 协议,序列化,反序列化,Json

    协议,序列化,反序列化,Json

    协议究竟是什么呢?首先得知道主机之间的网络通信交互的是什么数据,像平时使用聊天APP聊天可以清楚,用户看到的不仅仅是聊天的文字,还能够看到用户的头像昵称等其他属性。也就可以证明网络通信不仅仅是交互字符串那么简单。事实上网络通信还可能会通过一个结构

    2024年02月13日
    浏览(15)
  • 协议定制 + Json序列化反序列化

    协议定制 + Json序列化反序列化

    1.1 结构化数据 协议是一种 “约定”,socket api的接口, 在读写数据时,都是按 “字符串” 的方式来发送接收的。如果我们要传输一些\\\"结构化的数据\\\" 怎么办呢? 结构化数据: 比如我们在QQ聊天时,并不是单纯地只发送了消息本身,是把自己的头像、昵称、发送时间、消息本身

    2024年02月09日
    浏览(15)
  • iOS处理json,序列化和反序列化

    Mantle 是一个开源的 Objective-C 框架,用于在 iOS 和 macOS 应用程序中实现模型层的序列化和反序列化。它提供了一种简单而强大的方式来将 JSON数据格式转换为自定义的数据模型对象,以及将数据模型对象转换为字典或 JSON 格式。 Mantle具有如下特点 自动映射 Mantle自动将 JSON 数据

    2024年02月11日
    浏览(10)
  • rust学习-json的序列化和反序列化

    由于 serde 库默认使用 JSON 格式进行序列化和反序列化 因此程序将使用 JSON 格式对数据进行序列化和反序列化 JSON:广泛使用的 JavaScript 对象符号,用于许多 HTTP API Postcard:no_std 和嵌入式系统友好的紧凑二进制格式。 CBOR:用于小消息大小且无需版本协商的简洁二进制对象表示

    2024年02月12日
    浏览(11)
  • 【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )

    【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )

    当谈到网络编程时,序列化和反序列化是非常重要的概念。在上一篇文章中,我们已经了解了在Linux环境下实现简单的TCP网络程序的基础知识。本文将继续探讨序列化和反序列化,这些概念对于在网络上传输数据以及跨网络通信至关重要。通过深入了解序列化和反序列化,我

    2024年04月08日
    浏览(14)
  • 【网络】协议的定制与Json序列化和反序列化

    【网络】协议的定制与Json序列化和反序列化

    我们程序员写的一个个解决我们实际问题, 满足我们日常需求的网络程序, 都是在应用层 建立链接和断开链接 基于TCP协议,我们需要知道写代码时对应的接口大概在TCP通讯的过程中属于什么样的时间点角色,在TCP协议时详谈。三次握手,四次挥手 listen状态:准备好了,可以进

    2024年02月09日
    浏览(9)
  • JSON序列化与反序列化NULL值丢失问题

    做项目一般都会有一些特殊的需求,例如保留json中的null值,但是fastjson都会把null值得属性给过滤掉 json序列化保留null值 json反序列化保留null值 使用hutool的Json工具时

    2024年02月15日
    浏览(23)
  • 使用nlohmann json库进行序列化与反序列化

    nlohmann源码仓库:https://github.com/nlohmann/json 使用方式:将其nlohmann文件夹加入,包含其头文件json.hpp即可 demo

    2024年02月10日
    浏览(10)
  • 【网络编程】协议定制+Json序列化与反序列化

    【网络编程】协议定制+Json序列化与反序列化

    需要云服务器等云产品来学习Linux的同学可以移步/--腾讯云--/--阿里云--/--华为云--/官网,轻量型云服务器低至112元/年,新用户首次下单享超低折扣。   目录 一、序列化与反序列化的概念 二、自定义协议设计一个网络计算器 2.1TCP协议,如何保证接收方收到了完整的报文呢?

    2024年02月06日
    浏览(12)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包