VB.Net获取json字符串的元素

时间:2019-06-25 06:28:15

标签: vb.net

我向您解释: 由于有了此代码,我可以通过电子邮件/密码进入minecraft.net 插入RichTextBox中,我会收到带有结果的字符串json,例如用户名,clienttoken等, 问题是json字符串是未经格式化的给我的,像这样:

{“访问令牌”:“ a61723a4a4bb421ca576e1772d658fd1”,“客户端令牌”:“ 6c9d237d-8fbf-44ef-b46b-0b8a854bf391”,“ selectedProfile”:{“代理”:“ minecraft”,“ id”:“ 5552fc8c1cda1818”,“ “名称”:“ GoldenGamer098”,“用户ID”,“ 67bdcd7e3e77a59919844637f94ced63”,“ createdAt”:1534776382000,“ legacyProfile”:false,“ suspended”:false,“ TokenId”:“ 22447497”,“ paid”:true,迁移”:false}“ availableProfiles”:[{“代理”:“ minecraft”,“ id”:“ 5552fc8c0f954990b6ae788c18c3d1da”,“名称”:“ GoldenGamer098”,“用户ID”,“ 67bdcd7e3e77a59919844637f94ced63”,“ createdAt638” “ legacyProfile”:false,“ suspended”:false,“ TokenId”:“ 22447497”,“ paid”:true,“ migrated”:false}]}

它也给了我两次,我不知道为什么。 我需要做的是获取“ clienttoken”,“ name”,“ migrated”等各种内容并将其放在文本框中,我该怎么办?

私有ACCESS_TOKEN作为字符串

Public Function GetAccessToken() As String
    Return ACCESS_TOKEN
End Function

Public Sub ObtainAccessToken()
    Dim email As String = "Email"
    Dim password As String = "Password"
    Dim httpWebRequest = CType(WebRequest.Create("https://authserver.mojang.com/authenticate"), HttpWebRequest)
    httpWebRequest.ContentType = "application/json"
    httpWebRequest.Method = "POST"

    Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
        Dim json As String = "{""agent"":{""name"":""Minecraft"",""version"":1},""username"":""" & email & """,""password"":""" & password & """,""clientToken"":""6c9d237d-8fbf-44ef-b46b-0b8a854bf391""}"
        streamWriter.Write(json)
        streamWriter.Flush()
        streamWriter.Close()
        Dim httpResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)
        Dim stringSeparators() As String = {vbLf, vbCrLf}

        Using streamReader = New StreamReader(httpResponse.GetResponseStream())
            Dim result As String = streamReader.ReadToEnd()
            ACCESS_TOKEN = result
            RichTextBox1.Text = result
        End Using
    End Using
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ObtainAccessToken()
End Sub

0 个答案:

没有答案