none
How to post a JSON object using a Dictionary? RRS feed

  • Question

  • Here is the JSON that needs to be posted.

    { "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)

    The error I'm getting:

    Error reading JObject from JsonReader. Path'',line 0 postion 0

    Saturday, March 16, 2019 1:21 AM

All replies

  •   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
      
    Saturday, March 16, 2019 2:08 AM
  • Hi,

    I tried your code,it works well.Do you resolve the issue ?

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, March 18, 2019 8:47 AM