如何使用字典发布JSON对象?

时间:2019-03-15 03:20:33

标签: json vb.net

这是需要发布的JSON。

{
 "getcart": {
              "version": "1.0",
              "account": "6566",
              "username": "112",
              "password": "xxxxxxxx",
              "cartid": "57044d79",
} }

代码:

Dim jsonPost As New JsonPost("https://www.example.aspx")
Dim dictData As New Dictionary(Of String, Object)

dictData.Add("version", "1.0")
dictData.Add("account", "6566")
dictData.Add("username", "112")
dictData.Add("password", "xxxxxxxx")
dictData.Add("cartid", "57044d79")

Dim json As JObject = JObject.Parse(JsonPost.postData(dictData))
Dim response As String = JsonPost.postData(dictData)
MsgBox(response)

我得到的错误:

  

从JsonReader读取JObject时出错。路径'',第0行位置0

任何帮助都是有益的。

1 个答案:

答案 0 :(得分:0)

我无法使用字典来做到这一点。但是我能够将其发布为字符串。我搜索了2天以找到答案。最近,我觉得没有人可以帮助了。主要问题是在VB中声明包含对象的json字符串。人们最近不想帮忙,或者他们觉得这个问题很愚蠢。这是我对所有人的建议,如果您可以帮助您,不要回头。您永远不知道自己的答案可以为他人做什么。我试图为别人回答我的问题。这是我的操作方式:

          Dim JsonData As String = "{'getcart':{
                                                'version':'1.0',
                                                'account':'6566',
                                                'username':'112',
                                                'password':'xxxxxxxx',
                                                'cartid':'57044d79',
                                                 }}"



   Dim Uri As String = "https://www."
   Dim data = Encoding.UTF8.GetBytes(JsonData)
   Dim result_post = SendRequest(New Uri(Uri), data, "application/json", "POST")
   MsgBox(result_post)

 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