locked
How to break up string of key value pairs RRS feed

  • Question

  • User-1767698477 posted

    I have a variable that reads in a POST from paypal.  This is the instant payment notification stream and all the keys and values are stored into a string variable .

    <style type="text/css"><!-- BODY,.aolmailheader {font-size:10pt; color:black; font-family:Arial;} a.aolmailheader:link {color:blue; text-decoration:underline; font-weight:normal;} a.aolmailheader:visited {color:magenta; text-decoration:underline; font-weight:normal;} a.aolmailheader:active {color:blue; text-decoration:underline; font-weight:normal;} a.aolmailheader:hover {color:blue; text-decoration:underline; font-weight:normal;} --></style> mc_gross=0.05&protection_eligibility=Ineligible&payer_id=UEWAFF732VL&tax=0.00&payment_date=22%3A36%3A55+Jul+21%2C+2009+PDT&payment_status=Completed&charset=windows-1252&first_name=Scot

    This is part of the string.  How do I break this up into a set of variables.  I tried using request.form but this did not do anything.  I'm trying to get the variables and then pass them to a function that will write everything to a database.

    Dim amount = Request.Form("mc_gross")
                    Dim currency = Request.Form("currency")
                    Dim pmtdate = Request.Form("payment_date")
                    Dim status = Request.Form("payment_status")

    I only need certain variables from this variable which is strNewvalue. How to I get the values stored in this variable?


    Response.Write(IPNResponse)
                    Dim myEmail As MailMessage = New MailMessage()
                    myEmail.To = "e@aol.com"
                    myEmail.From = "angers.com"
                    myEmail.Body = strNewValue
                    myEmail.Subject = IPNResponse & " IPN"
                    SmtpMail.SmtpServer = "mail.link-exchangers.com"
                    SmtpMail.Send(myEmail)

    Wednesday, July 22, 2009 10:10 PM

Answers

  • User1006193418 posted

    You're code doesn't work.  DOes anyone know how to get the variables form the POST? Please post code in VB and NOT c#

    Hi,

    It seems that the string in your first post is a querystring instead of form post. You can easily have this test.

    1. Create a new web page named test.aspx

    2. Write these code into the Page_Load event of the test.aspx page.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim amount = Request.Form("mc_gross")
        Dim currency = Request.Form("currency")
        Dim pmtdate = Request.Form("payment_date")
        Dim status = Request.Form("payment_status")
        Dim charset = Request.Form("charset")
        Dim first_name = Request.Form("first_name")
    End Sub

    3. Use this URL to visit test.aspx: add a question mark after .aspx extension and append the string to the question mark.

    test.aspx?mc_gross=0.05&protection_eligibility=Ineligible&payer_id=UEWAFF732VL&tax=0.00&payment_date=22%3A36%3A55+Jul+21%2C+2009+PDT&payment_status=Completed&charset=windows-1252&first_name=Scot

    Of course, we can also retrieve the data from this string via this example if it is a querystring.

    Dim input As String = "mc_gross=0.05&protection_eligibility=Ineligible&payer_id=UEWAFF732VL&tax=0.00&payment_date=22%3A36%3A55+Jul+21%2C+2009+PDT&payment_status=Completed&charset=windows-1252&first_name=Scot"
    input = Server.UrlDecode(input)
    
    Dim hash As New Hashtable
    Dim arr() As String = input.Split("&")
    
    For Each pair As String In arr
        Dim key, value As String
        key = pair.Substring(0, pair.IndexOf("="))
        value = pair.Substring(pair.IndexOf("=") + 1)
        hash.Add(key, value)
    Next

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2009 7:41 AM
  • User1006193418 posted

    Hi, Thanks for your response.  How do I access these as variables in my code?I need to assign these variables to pmt and then call a stored procedure.  Also, the paiddate needs to be a datetime format as I defined in my table.  How would I convert that? CType( ?  Most of the others are integer or string.

    Hi,

    Since we have already get a hash table which stores these values, we can easily access them by this way.

    'Append to the code in my previous post 
    Dim mc_gross As String = hash.Item("mc_gross") 
    Dim payer_id As String = hash.Item("payer_id") 
    Dim payment_status As String = hash.Item("payment_status")
    
    

    However, because all of these values are obtain from strings, we need to do the convert ourselves like this.

    Dim payment_date As DateTime = CType(hash.Item("payment_date"), DateTime)
    
    

    Please kindly note that some value maybe cannot convert correctly. Like the code above, it tries to convert the string of payment_date into DateTime. However, in the input string, the payment_date is "22:36:55 Jul 21, 2009 PDT" and for there is a "PDT" in the string, it will throw a error when we try to convert it to DateTime.

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 30, 2009 8:09 AM

All replies

  • User2136026738 posted

     Hi,

    Here the code which will do this and have the values in hashtable

    System.Collections.Hashtable hs1 = new System.Collections.Hashtable();
                    string str = "

    mc_gross=0.05&protection_eligibility=Ineligible&payer_id=UEWAFF732VL&tax=0.00&payment_date=22%3A36%3A55+Jul+21%2C+2009+PDT&payment_status=Completed&charset=windows-1252&first_name=Scot

    ";
                    while (str != null || str != "")
                    {

                        int i = str.IndexOf('=');
                        string key = str.Substring(0, i);

                        str = str.Remove(0, i + 1);
                        i = str.IndexOf('&');
                        string value = str.Substring(0, i);
                        str = str.Remove(0, i + 1);

                        hs1.Add(key, value);
                    }

     

    But this code is in C# please convert it to VB.

    Thursday, July 23, 2009 1:30 AM
  • User2136026738 posted

     Hi

    I forgot to check the value for last key value pair. This will fail for the last key value pair.You have to make a check if i = str.IndexOf('&'); returns -1 then you have to assigne the rest of the field to value and clear the str so that the wile loop breaks.

    Let me know if you have any problem.

    Thursday, July 23, 2009 1:34 AM
  • User-1767698477 posted

    I converted your code to vb.  I have not added the last key as you said . Can you show me how to add that?  Also, hs1 is underlined.  It's not declared.  It would appear you're going to store the key value pair here?In order to get the variables to work in my function, which I showed you above, I need to be able to pass them through to the function at the bottom.  How is this getting done?


                    While (strNewValue IsNot Nothing OrElse strNewValue <> "")
                        Dim i As Integer = strNewValue.IndexOf("="c)
                        Dim key As String = strNewValue.Substring(0, i)
                        strNewValue = strNewValue.Remove(0, i + 1)
                        i = strNewValue.IndexOf("&"c)
                        Dim value As String = strNewValue.Substring(0, i)
                        strNewValue = strNewValue.Remove(0, i + 1)
                        hs1.Add(key, value)

                    End While

    Friday, July 24, 2009 1:05 AM
  • User-1767698477 posted

    You're code doesn't work.  DOes anyone know how to get the variables form the POST? Please post code in VB and NOT c#

    Friday, July 24, 2009 2:30 AM
  • User1006193418 posted

    You're code doesn't work.  DOes anyone know how to get the variables form the POST? Please post code in VB and NOT c#

    Hi,

    It seems that the string in your first post is a querystring instead of form post. You can easily have this test.

    1. Create a new web page named test.aspx

    2. Write these code into the Page_Load event of the test.aspx page.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim amount = Request.Form("mc_gross")
        Dim currency = Request.Form("currency")
        Dim pmtdate = Request.Form("payment_date")
        Dim status = Request.Form("payment_status")
        Dim charset = Request.Form("charset")
        Dim first_name = Request.Form("first_name")
    End Sub

    3. Use this URL to visit test.aspx: add a question mark after .aspx extension and append the string to the question mark.

    test.aspx?mc_gross=0.05&protection_eligibility=Ineligible&payer_id=UEWAFF732VL&tax=0.00&payment_date=22%3A36%3A55+Jul+21%2C+2009+PDT&payment_status=Completed&charset=windows-1252&first_name=Scot

    Of course, we can also retrieve the data from this string via this example if it is a querystring.

    Dim input As String = "mc_gross=0.05&protection_eligibility=Ineligible&payer_id=UEWAFF732VL&tax=0.00&payment_date=22%3A36%3A55+Jul+21%2C+2009+PDT&payment_status=Completed&charset=windows-1252&first_name=Scot"
    input = Server.UrlDecode(input)
    
    Dim hash As New Hashtable
    Dim arr() As String = input.Split("&")
    
    For Each pair As String In arr
        Dim key, value As String
        key = pair.Substring(0, pair.IndexOf("="))
        value = pair.Substring(pair.IndexOf("=") + 1)
        hash.Add(key, value)
    Next

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2009 7:41 AM
  • User-1767698477 posted

    Hi, Thanks for your response.  How do I access these as variables in my code?I need to assign these variables to pmt and then call a stored procedure.  Also, the paiddate needs to be a datetime format as I defined in my table.  How would I convert that? CType( ?  Most of the others are integer or string.


    Dim Pmt As New Payment
    Pmt.Amount = amount
    Pmt.Currency = currency
    Pmt.PaidDate = pmtdate
    Pmt.Status = status
    Pmt.UserID = payerID
    Pmt.PayerEmail = payer_email
    Pmt.TransactionID = txn_id
    Pmt.Description = item
    Pmt.SellerEmail = Receiver_email
    'add record to database
    Dim pid As Integer = Payment.Add(Pmt) 'this line calls the function to
    add a payment to the table

    Thursday, July 30, 2009 3:04 AM
  • User1006193418 posted

    Hi, Thanks for your response.  How do I access these as variables in my code?I need to assign these variables to pmt and then call a stored procedure.  Also, the paiddate needs to be a datetime format as I defined in my table.  How would I convert that? CType( ?  Most of the others are integer or string.

    Hi,

    Since we have already get a hash table which stores these values, we can easily access them by this way.

    'Append to the code in my previous post 
    Dim mc_gross As String = hash.Item("mc_gross") 
    Dim payer_id As String = hash.Item("payer_id") 
    Dim payment_status As String = hash.Item("payment_status")
    
    

    However, because all of these values are obtain from strings, we need to do the convert ourselves like this.

    Dim payment_date As DateTime = CType(hash.Item("payment_date"), DateTime)
    
    

    Please kindly note that some value maybe cannot convert correctly. Like the code above, it tries to convert the string of payment_date into DateTime. However, in the input string, the payment_date is "22:36:55 Jul 21, 2009 PDT" and for there is a "PDT" in the string, it will throw a error when we try to convert it to DateTime.

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 30, 2009 8:09 AM
  • User-1767698477 posted

    Ok, thanks Shengging.

    I'll try this and maybe using substring to remove the last 4 characters. II'll verify that there are only 3 characters like this. PST, EST, GMT, etc.

    Thursday, July 30, 2009 10:11 PM
  • User1006193418 posted

    Ok, thanks Shengging.

    I'll try this and maybe using substring to remove the last 4 characters. I'll verify that there are only 3 characters like this. PST, EST, GMT, etc.

    Hi,

    That will be great.

    Regards,
    Shengqing Yang

    Friday, July 31, 2009 2:32 AM
  • User-1767698477 posted


    Friday, July 31, 2009 3:56 AM