這個(gè)代碼寫(xiě)的有點(diǎn)時(shí)間了,可能有點(diǎn)小bug,歡迎評(píng)論區(qū)反饋
作用是將Json文本轉(zhuǎn)化成一個(gè)HarryNode類(lèi)進(jìn)行相關(guān)的Json對(duì)象處理或者讀取,也可以將一個(gè)HarryNode對(duì)象用ToString變?yōu)镴son文本。
舉例:
1、讀取節(jié)點(diǎn)數(shù)據(jù)
dim harryNode = New HarryNode("", "{""msg"":""hello world!""}")
msgbox(harryNode.GetAtt("msg")) '彈窗顯示hello world!
'下面展示更復(fù)雜的嵌套讀取
dim harryNode = New HarryNode("", "{""node1"": {""msg"":""hello world!""}}")
msgbox(harryNode.GetAtt("node1.msg")) '彈窗顯示hello world! 沒(méi)錯(cuò),用“.”作為路徑連接符進(jìn)行尋址
'如果json的鍵里包含“.”可以將源碼里的“.”替換成其它字符,也可以這樣進(jìn)行取值
msgbox(harryNode.GetAtt("node1")("msg").value
'這里的harryNode.GetAtt("node1")返回的是一個(gè)字典對(duì)象(String, HarryNode)
2、創(chuàng)建新Json節(jié)點(diǎn),寫(xiě)入數(shù)據(jù)并輸出文本
Dim harryNode = New HarryNode("", "{}")
harryNode.SetAtt("msg", """hello world!""")
MsgBox(harryNode.ToString)
'可以看到SetAtt方法的第二個(gè)參數(shù)輸入的字符串需要是json字符串格式,因此字符串本身需要加雙引號(hào)
'下面可以用SetAtt的另一種重載方法,與上面代碼的結(jié)果相同
harryNode.SetAtt("msg", "hello world!", NodeTypeEnum.isString)
MsgBox(harryNode.ToString)
'同樣,對(duì)嵌套的復(fù)雜json對(duì)象,可以如下
harryNode.SetAtt("node1.msg", "hello world!", NodeTypeEnum.isString)
'下面這樣寫(xiě)也是可以的
harryNode.SetAtt("node1", "{""msg"": ""hello world!""}")
文檔
1、方法和函數(shù)
New
構(gòu)造函數(shù)
name | String | 節(jié)點(diǎn)的名字(對(duì)于根節(jié)點(diǎn)此項(xiàng)沒(méi)啥意義) |
json | String | 要解析構(gòu)造的JSON串 |
parent | HarryNode | 實(shí)例的父節(jié)點(diǎn) |
name | String | 節(jié)點(diǎn)的名字(對(duì)于根節(jié)點(diǎn)此項(xiàng)沒(méi)啥意義) |
nodeValue | Object | 節(jié)點(diǎn)VB.NET對(duì)象值 |
type | NodeTypeEnum | 節(jié)點(diǎn)值的類(lèi)型 |
parent | HarryNode | 實(shí)例的父節(jié)點(diǎn) |
GetAtt
獲得指定路徑的VB.NET對(duì)象
path | String | 節(jié)點(diǎn)路徑 |
defaultValue | Object | 沒(méi)有獲取到返回的值,默認(rèn)Nothing |
SetAtt
根據(jù)指定路徑設(shè)置節(jié)點(diǎn)值
path | String | 節(jié)點(diǎn)路徑 |
newValue | Object | 節(jié)點(diǎn)的值(VB.NET對(duì)象) |
newValueType | NodeTypeEnum | 值的類(lèi)型 |
path | String | 節(jié)點(diǎn)路徑 |
json | String | 節(jié)點(diǎn)的值(JSON字符串) |
ReName
重命名某個(gè)節(jié)點(diǎn)
path | String | 節(jié)點(diǎn)路徑 |
newName | String | 新名字 |
ToJson
返回JSON字符串,與ToString()等價(jià)
GetNode
獲得指定路徑的HarryNode對(duì)象
path | String | 節(jié)點(diǎn)路徑 |
AddNode
添加子節(jié)點(diǎn)
path | String | 節(jié)點(diǎn)路徑 |
nodeName | String | 子節(jié)點(diǎn)名 |
nodeJson | String | 子節(jié)點(diǎn)JSON串 |
Del
刪除指定路徑的節(jié)點(diǎn)
path | String | 節(jié)點(diǎn)路徑 |
Merge
合并兩個(gè)字典節(jié)點(diǎn);
node | HarryNode | 要合并的節(jié)點(diǎn) |
GetChildPath
返回一個(gè)當(dāng)前節(jié)點(diǎn)子節(jié)點(diǎn)名的列表
Add
指定某個(gè)節(jié)點(diǎn)的數(shù)據(jù)加一個(gè)值
path | String | 節(jié)點(diǎn)路徑 |
addValue | Single | 加數(shù) |
ConAdd
指定某個(gè)節(jié)點(diǎn)的數(shù)據(jù)加一個(gè)值,但是限制了數(shù)的范圍
path | String | 節(jié)點(diǎn)路徑 |
addValue | Single | 加數(shù) |
maxValue | Single | 最大值 |
minValue | Single | 最小值,默認(rèn)0 |
Mul
指定某個(gè)節(jié)點(diǎn)的數(shù)據(jù)乘一個(gè)值
path | String | 節(jié)點(diǎn)路徑 |
addValue | Single | 乘數(shù) |
Power
指定某個(gè)節(jié)點(diǎn)的數(shù)據(jù)求次冪
path | String | 節(jié)點(diǎn)路徑 |
addValue | Single | 冪 |
2、屬性
Value
當(dāng)前節(jié)點(diǎn)的VB.NET類(lèi)型值
3、事件
NodeContentChangeBefore
節(jié)點(diǎn)內(nèi)容改變之前
path | String | 節(jié)點(diǎn)路徑 |
newValue | Object | 即將變成的值 |
newValueType | NodeTypeEnum | 即將變成值的類(lèi)型 |
NodeContentChangeBeforeFromJson
節(jié)點(diǎn)內(nèi)容改變之前(通過(guò)JSON解釋?zhuān)?/p>
path | String | 節(jié)點(diǎn)路徑 |
json | String | 即將變成的值的JSON字符串 |
NodeContentChangeLater
節(jié)點(diǎn)內(nèi)容改變之后
path | String | 節(jié)點(diǎn)路徑 |
newValue | Object | 變成的值 |
newValueType | NodeTypeEnum | 變成值的類(lèi)型 |
NodeContentChangeLaterFromJson文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-724254.html
節(jié)點(diǎn)內(nèi)容改變之后(通過(guò)JSON解釋?zhuān)?span toymoban-style="hidden">文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-724254.html
path | String | 節(jié)點(diǎn)路徑 |
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
'數(shù)值
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
'未在字符串內(nèi)時(shí)
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
'第一層嵌套內(nèi)
colon += 1
ElseIf bigBrackets > 1 OrElse brackets > 0 Then
temp.Add(c)
End If
Case """"
If bigBrackets = 1 AndAlso brackets = 0 Then
'第一層嵌套內(nèi)
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
'第一層嵌套內(nèi)
'在字符串內(nèi)
Select Case c
Case """"
temp.Add(c)
If escape = 1 Then
'轉(zhuǎn)義"
escape = 0
Else
doubleQuotationMark = 0
If colon = 0 Then
'節(jié)點(diǎn)名
stringContent = Join(temp.ToArray, "")
temp.Clear()
name = stringContent.Substring(1, stringContent.Length - 2)
End If
End If
Case "\"
escape += 1
If escape > 1 Then
'轉(zhuǎn)義\
temp.Add(c)
escape = 0
End If
Case "n"
If escape = 1 Then
'轉(zhuǎn)義換行
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
'未在字符串內(nèi)時(shí)
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
'第一層嵌套內(nèi)
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
'第一層嵌套內(nèi)
'在字符串內(nèi)
Select Case c
Case """"
temp.Add(c)
If escape = 1 Then
'轉(zhuǎn)義"
escape = 0
Else
doubleQuotationMark = 0
End If
Case "\"
escape += 1
If escape > 1 Then
'轉(zhuǎn)義\
temp.Add(c)
escape = 0
End If
Case "n"
If escape = 1 Then
'轉(zhuǎn)義換行
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
到了這里,關(guān)于【vb.net】輕量JSON序列及反序列化的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!