看到有人在.net2.0中无法利用类库来解析json数据,而网上居然有大量的文章实现都非常复杂:有的是.dll来实现,有的XXXX,其实这是一个非常简单的字符串解析,
vb.net代码函数如下,复制到你的程序中立即可以使用。
json.vb
----------------
Imports System.Text.RegularExpressions
Public Class Json
''' <summary>
''' 将Json数据转化为二维数据返回 added by chenrenqi 2014-10-8
''' </summary>
''' <param name="json_str">从http返回的Json字符串</param>
''' <returns>二维数据返回,第一维为普通索引数据,第二维为关联数组(hashtable)</returns>
''' <remarks></remarks>
Public Shared Function jsonToArray(ByVal json_str As String) As List(Of Hashtable)
' str=[{"kefu_id":"1","kefu_name":"kefu","is_online":"0","is_chating":"0"},{"kefu_id":"2","kefu_name":"robot","is_online":"0","is_chating":"0"},{"kefu_id":"3","kefu_name":"admin","is_online":"0","is_chating":"0"}]
Dim _patternString As String = "{([^\}]+)}"
Dim reg As Regex = New Regex(_patternString, RegexOptions.IgnoreCase)
Dim match As Match
Dim result As New List(Of Hashtable)
match = reg.Match(json_str)
Dim value As String
Dim key As String
While match.Success
value = match.Value().Substring(1, (match.Value().Length - 2)) '去掉首尾{}
Dim values() As String = Split(value, ",")
Dim val As String
Dim vals() As String
Dim htable As New Hashtable
For Each val In values
vals = Split(val, """:""")
If vals.Length < 2 Or val Is Nothing Then
Continue For
End If
key = JsonConv(vals(0))
value = JsonConv(vals(1))
htable(key.Substring(1, key.Length - 1)) = value.Substring(0, value.Length - 1)
Next
result.Add(htable)
match = match.NextMatch()
End While
Return result
End Function
''' <summary>
''' Json编码(unicode编码)的转化中文
''' </summary>
''' <param name="json"></param>
''' <returns></returns>
''' <remarks></remarks>
Shared Function JsonConv(ByVal json As String) As String
If json.IndexOf("\u") = -1 Then
Return json '不用转换
End If
Dim str_len = Len(json)
json = Replace(json, "\u", "&H")
Dim result = ""
For i As Int16 = 1 To (str_len \ 6)
result = result & ChrW(CLng(Mid(json, 6 * i - 5, 6)))
Next
Return result
End Function
End Class
by the way :本人.net 编程有6年经验,现在团队缺少vb.net人才 ,有找vb.net方面工作,可以加QQ811470699 注明vb.net 和作者 一起提高学习。
------解决思路----------------------
在你 vs 开发工具的“程序包管理器->程序包管理控制台”下打入
install-package Newtonsoft.Json
它会自动加载适合你的工程的版本的内裤。
------解决思路----------------------
才知道,多谢