使用Newtonsoft在VB.net中编写和读取JSON

时间:2016-08-05 07:35:20

标签: json vb.net json.net

这是我第一次使用JSON而且我被卡住了。我正在尝试通过http://sms.roamtech.com/smsapi/发送测试短信。格式为:

发送邮件格式(json)。

{
    "result":{
        "account":"xxxx",
        "user":"xxxx",
        "password":"xxxxxxxx",
        "requestType":"BULK",
        "alphanumeric":"xxxxxxxx",
        "data":{
            "linkid":"xxxxxxx",
            "msisdn":"xxxxxxxxxxx",
            "networkid":"1",
            "message":"xxxxxxxxxxxx",
            "callback":"http//test"
        }
    }
}   

所以,这是我在审核了本网站和其他网站上的各种帖子后想出来的:

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.IO
Imports System.Net
Imports System.Text

Module modJSON
     Public Class clsResult
         Public account As String
         Public user As String
         Public password As String
         Public requestType As String
         Public alphanumeric As String
         Public data As New clsData
     End Class
     Public Class clsData
        Public linkid As String
        Public msisdn As String
        Public networkid As String
        Public message As String
        Public callback As String
    End Class
    Public Class clsPOST
        Public result As New clsResult

    End Class

    Public Sub chkJSON()
        Dim r As New clsResult
        Dim d As New clsData
        Dim x As New clsPOST

        r.account = "8852"
        r.user = "username"
        r.password = "password"
        r.requestType = "BULK"
        r.alphanumeric = "SMSLEO"


        d.linkid = "1001"
        d.msisdn = "2547xxxxxxxx"
        d.networkid = "1"
        d.message = "Just a test"
        d.callback = "http://infiniti-africa.com/json"

        r.data = d
        x.result = r

        Dim uriRoam As New Uri("http://sms.roamtech.com/smsapi")
        Dim strJSON = JsonConvert.SerializeObject(x, Formatting.Indented)
        Dim bytJSON = Encoding.UTF8.GetBytes(strJSON)

        Dim result_post = SendRequest(uriRoam, bytJSON, "application/json", "POST")
        MsgBox(result_post)

    End Sub

    Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
        Dim req As WebRequest = WebRequest.Create(uri)
        req.ContentType = contentType
        req.Method = method
        req.ContentLength = jsonDataBytes.Length


        Dim stream = req.GetRequestStream()
        stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
        stream.Close()

        Dim response = req.GetResponse().GetResponseStream()

        Dim reader As New StreamReader(response)
        Dim res = reader.ReadToEnd()
        reader.Close()
        response.Close()

        Return res
    End Function
End Module

字符串strJSON似乎包含正确的键:值组合。但是,代码不会发送测试短信,我也没有得到任何回复。 'SendRequest'返回一个空字符串。

此外,我不确定将什么用于“回调”网址,这是转发投放报告的位置。

注意: 1. "linkid"是唯一的消息ID 2. "msidn"是收件人电话号码

感谢任何帮助。

我也尝试过使用以下类:

Public Class JsonPost

    Private urlToPost As String = ""

    Public Sub New(ByVal urlToPost As String)
        Me.urlToPost = urlToPost
    End Sub

    Public Function postData(pstData As Byte()) As Boolean
        Dim webClient As New WebClient()
        Dim resByte As Byte()
        Dim resString As String

        Try
            webClient.Headers("content-type") = "application/json"
            resByte = webClient.UploadData(Me.urlToPost, "post", pstData)
            resString = Encoding.Default.GetString(resByte)
            Console.WriteLine(resString)
            webClient.Dispose()
            Return True
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        Return False
    End Function

End Class

然后致电:

    Dim strJSON = JsonConvert.SerializeObject(x)
    Dim bytJSON = Encoding.UTF8.GetBytes(strJSON)

    Dim jsonPost As New JsonPost("http://sms.roamtech.com/smsapi")
    jsonPost.postData(bytJSON)

我仍然一无所获。现在已经挣扎了三天了。任何人的想法?

1 个答案:

答案 0 :(得分:0)

原来我一直在用尾随斜线(/)强调四天。 API网址为:

  

http://sms.roamtech.com/smsapi/

我一直在使用:

  

http://sms.roamtech.com/smsapi

经验教训。