标签 VB.net 下的文章

1.安装Newtonsoft.JSON

在【解决方案资源管理器】中右键项目,选择【管理 NuGet 程序包】

image

在浏览页面中搜索并安装Newtonsoft.Json即可

image

2. 导入Newtonsoft.Json

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

3.示例

json如下

{
    "message": null,
    "data": {
        "paginationData": [
            {
                "name": "管理员",
                "user_id": 16394,
                "groupsname": ""
            },
            {
                "name": "测试",
                "user_id": 16395,
                "groupsname": "部门A"
            },
            {
                "name": "隔壁老王",
                "user_id": 16396,
                "groupsname": "部门A"
            },
            {
                "name": "小明",
                "user_id": 16397,
                "groupsname": "部门B"
            }
        ],
        "result": 1
    }
}

image

先创建一个对应的结构体

Public Structure Employee
    Dim name As String
    Dim user_id As Integer
    Dim groupsname As String
End Structure

读取并解析json

Dim rawStr As String = System.IO.File.ReadAllText("D:\test.json")           '读取json文件,可根据实际情况替换对应语句
Dim p As JObject = CType(JsonConvert.DeserializeObject(rawStr), JObject)    '用json文本创建对象

MsgBox(p("message".ToString))                                               '读取值

Dim emp As List(Of Employee)                                                                        '创建对应的结构体列表
emp = JsonConvert.DeserializeObject(Of List(Of Employee))(p("data")("paginationData").ToString)     '如果有多层结构,可以继续添加p的子孙项
Dim empCount As Integer = emp.Count     '全部职员数量
MsgBox(emp(2).name & emp(2).user_id & emp(2).groupsname)    '结果为“隔壁老王16396部门A”

方法一

Public Function getWebSource(ByVal url As String) As String
    Try
        Dim stream As IO.Stream = WebRequest.Create(url).GetResponse().GetResponseStream()
        Dim sr As StreamReader = New StreamReader(stream, System.Text.Encoding.UTF8)
        Return sr.ReadToEnd()
    Catch ex As Exception
        Return ""
        'Return ex.ToString()
    End Try
End Function

方法二

节选自网络,实测较慢

Public Function GetWebCode(ByVal strURL As String) As String
    Dim httpReq As System.Net.HttpWebRequest
    Dim httpResp As System.Net.HttpWebResponse
    Dim httpURL As New System.Uri(strURL)
    Dim ioS As System.IO.Stream, charSet As String, tCode As String
    Dim k() As Byte
    ReDim k(0)
    Dim dataQue As New Queue(Of Byte)
    httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
    Dim sTime As Date = CDate("1990-09-21 00:00:00")
    httpReq.IfModifiedSince = sTime
    httpReq.Method = "GET"
    httpReq.Timeout = 7000

    Try
        httpResp = CType(httpReq.GetResponse(), HttpWebResponse)
    Catch
        Debug.Print("weberror")
        GetWebCode = "<title>no thing found</title>" : Exit Function
    End Try
    '以上为网络数据获取
    ioS = CType(httpResp.GetResponseStream, Stream)
    Do While ioS.CanRead = True
        Try
            dataQue.Enqueue(ioS.ReadByte)
        Catch
            Debug.Print("read error")
            Exit Do
        End Try
    Loop
    ReDim k(dataQue.Count - 1)
    For j As Integer = 0 To dataQue.Count - 1
        k(j) = dataQue.Dequeue
    Next
    '以上,为获取流中的的二进制数据
    tCode = Encoding.GetEncoding("UTF-8").GetString(k) '获取特定编码下的情况,毕竟UTF-8支持英文正常的显示
    charSet = Replace(GetByDiv2(tCode, "charset=", """"), """", "") '进行编码类型识别
    '以上,获取编码类型
    If charSet = "" Then 'defalt
        If httpResp.CharacterSet = "" Then
            tCode = Encoding.GetEncoding("UTF-8").GetString(k)
        Else
            tCode = Encoding.GetEncoding(httpResp.CharacterSet).GetString(k)
        End If
    Else
        tCode = Encoding.GetEncoding(charSet).GetString(k)
    End If
    Debug.Print(charSet)
    'Stop
    '以上,按照获得的编码类型进行数据转换
    '将得到的内容进行最后处理,比如判断是不是有出现字符串为空的情况
    GetWebCode = tCode
    If tCode = "" Then GetWebCode = "<title>no thing found</title>"
End Function

Public Function GetByDiv2(ByVal code As String, ByVal divBegin As String, ByVal divEnd As String) '获取分隔符所夹的内容[完成,未测试]
'仅用于获取编码数据
    Dim lgStart As Integer
    Dim lens As Integer
    Dim lgEnd As Integer
    lens = Len(divBegin)
    If InStr(1, code, divBegin) = 0 Then GetByDiv2 = "" : Exit Function
    lgStart = InStr(1, code, divBegin) + CInt(lens)

    lgEnd = InStr(lgStart + 1, code, divEnd)
    If lgEnd = 0 Then GetByDiv2 = "" : Exit Function
    GetByDiv2 = Mid(code, lgStart, lgEnd - lgStart)
End Function