0%

C# VB .NET 解析 JSON

如何在 .NET 程序中解析 JSON 使用 Newtonsoft.Json

第三方类来操纵 Newtonsoft.Json 是.NET 下开源的json格式序列号和反序列化的类库

官方网站: http://json.codeplex.com/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Imports System.Text.RegularExpressions
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
''' <summary>
''' json序列化
''' </summary>
Public Class json
''' <summary>
''' 解析JSON数据
''' </summary>
''' <param name="empData">源数据</param>
''' <param name="key">指定值下的数据,可省略。</param>
''' <returns>0 非 json 数据</returns>
Public Shared Function JsonDecode(ByVal empData, Optional ByVal key1 = Nothing, Optional ByVal key2 = Nothing, Optional ByVal arry_i = Nothing) As String
Try
If IsJson(empData) = False Then Return 0

If arry_i IsNot Nothing Then
'http://blog.csdn.net/goodelephant/article/details/24769421
Return CType(JsonConvert.DeserializeObject(empData), JObject)(key1)(arry_i)(key2).ToString() '这个是解析数组 下标。。
End If


If key1 = Nothing Then Return CType(JsonConvert.DeserializeObject(empData), JObject).ToString

If key2 = Nothing Then Return CType(JsonConvert.DeserializeObject(empData), JObject)(key1).ToString()

Return CType(JsonConvert.DeserializeObject(empData), JObject)(key1)(key2).ToString()


Catch ex As Exception
Return ex.Message
End Try
End Function
End Class
1
2
3
4
5
6
7
8
9
10
11
12
13
''' <summary>
''' 序列化 json
''' </summary>
''' <param name="str">对象</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function JsonEncode(ByVal str As Object) As String
Try
Return JsonConvert.SerializeObject(str)
Catch ex As Exception
Return ex.Message
End Try
End Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Public Shared Function JsonSerializeMore_HttpJson(ByVal code As String, ByVal msg As String, Optional ByVal data As Object = Nothing)
'http://bbs.csdn.net/topics/390910345
'http://www.cnblogs.com/08shiyan/p/3464028.html
' Dim cusList As New List(Of httpjson)()
'Dim cusList As New List(Of HttpJson)

'cusList.Add(New HttpJson("6902083881405", "娃哈哈饮用纯净水", ""))
'cusList.Add(New HttpJson("6902083893736", "娃哈哈营养快线原味"))

'Dim dic As Dictionary(Of String, HttpJson) = New Dictionary(Of String, HttpJson)
'dic.Add("1", New HttpJson("1", "2", 1.1))

''dic.Add("2222", New HttpJson("1", "2", 1.1))
'' dic.Add("3333", New HttpJson("1", "2", 1.1))
'Dim jsonStr As String = Newtonsoft.Json.JsonConvert.SerializeObject(dic)
'MsgBox(jsonStr)


'MsgBox(JsonConvert.SerializeObject(cusList))


Return JsonEncode(New HttpJson(code, data))



End Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
''' <summary>
''' json转换datatable
''' </summary>
''' <param name="strJson"></param>
''' <returns>返回 0 非json 数据</returns>
Public Shared Function JsonToDataTable(ByVal strJson As String) As DataTable

Dim rg As New Regex("(?<={)[^:]+(?=:\\[)", RegexOptions.IgnoreCase)
Dim strName As String = rg.Match(strJson).Value
Dim dt As DataTable = Nothing
strJson = strJson.Substring((strJson.IndexOf("[") + 1))
strJson = strJson.Substring(0, strJson.IndexOf("]"))
Dim matchs As MatchCollection = New Regex("(?<={)[^}]+(?=})").Matches(strJson)
Dim i As Integer
For i = 0 To matchs.Count - 1
Dim strRow As String = matchs(i).Value
Dim strRows As String() = Split(strRow, ",")

If (dt Is Nothing) Then
dt = New DataTable
dt.TableName = strName
For Each str As String In strRows
Dim dc As New DataColumn
Dim strCell As String() = Split(str, ":")
dc.ColumnName = strCell(0).Replace("""", "")
dc.ColumnName = Trim(dc.ColumnName.Replace(Chr(13), ""))
dc.ColumnName = Trim(dc.ColumnName.Replace(Chr(10), ""))
dt.Columns.Add(dc)
Next
dt.AcceptChanges()
End If

Dim dr As DataRow = dt.NewRow()

For j As Integer = 0 To strRows.Length - 1
dr(j) = Trim(strRows(j).Split(":")(1).Replace("""", ""))
Next j
dt.Rows.Add(dr)
dt.AcceptChanges()
Next i
Return dt
End Function

''' <summary>
''' 验证是否 JSON 数据
''' </summary>
''' <param name="input"></param>
''' <returns></returns>
Public Shared Function IsJson(ByVal input As String) As Boolean

If input = "" Then Return False '为空字符时会出现异常,这里直接返回,不进行检测
input = input.Trim
Return ((input.StartsWith("{") AndAlso input.EndsWith("}")) OrElse (input.StartsWith("[") AndAlso input.EndsWith("]")))
End Function
1
2


觉得文章有用?请我喝杯咖啡~