none
Parsing Web Service Rest api trouble RRS feed

  • Question

  • Dear all,

    I need to connect to a web service and send some parameters in order to get result back.

    The string request I need to send to the URl is as follow:

    <tt>{"loanAmount":"500,000.00","interestRate":"5.25","loanLength":"25","rewardScheme":"flybuys","result":""}</tt>

    The value for each parmeter are coming from a suer interface selection, so I will need to pass them as parameters. In order to do so I transform the REQUEST string as follow:

    private const string REWARDDATA = @"{""loanAmount"":""{0}"",""interestRate"":""5.25"",""loanLength"":""{1}"",""rewardScheme"":""{2}"",""result"":""""}";
    

    Then as a test I was passing parameters as bellow:

    string REWARDDATAREQUEST=String.Format(REWARDDATA,"500,000.00","25","flybuys");

    By doing so it return a bad format issue on the String.Format sequence that I could not identify.

    Any idea what is wrong with that?

    An other question regarding the service request, when i check in debug mode the string it is in a form :

    "{\"loanAmount\":\"{0}\",\"interestRate\":\"5.25\",\"loanLength\":\"{1}\",\"rewardScheme\":\"{2}\",\"result\":\"\"}"

    What are those "\" caracters that it place ? I need to understand this because the result string is return in that form. How should I interprete it ?

    regards

    serge


    Your knowledge is enhanced by that of others.

    • Moved by Bob Shen Monday, March 25, 2013 8:16 AM
    Saturday, March 23, 2013 10:10 AM

Answers

  • I believe that you are running into problems with your string format because of the first and last brace surrounding the entire string.  String.Format needs to find {} pairs and interpret what is between them.  Remove the outer {} pair from REWARDDATA, and try your Format function call.  After the call prepend and append the {} pair.

    The debugger always shows escaped format strings, such as you would use in code, thus the reason for the \".  This would actually be my preferred way of building strings with quotes, rather than the double quote style you have used, meaning I prefer \" to "", which are mostly equivalent.

    Mike

    Saturday, March 23, 2013 11:31 AM
  • It might just be better to split the results on the comma character, then split each part on the colon character.  You might also search for a json parser, as the data you are showing is a json response string.  I suspect, though I have never tried, that there exists something in .Net.

    Mike

    Monday, March 25, 2013 8:48 PM

All replies

  • I believe that you are running into problems with your string format because of the first and last brace surrounding the entire string.  String.Format needs to find {} pairs and interpret what is between them.  Remove the outer {} pair from REWARDDATA, and try your Format function call.  After the call prepend and append the {} pair.

    The debugger always shows escaped format strings, such as you would use in code, thus the reason for the \".  This would actually be my preferred way of building strings with quotes, rather than the double quote style you have used, meaning I prefer \" to "", which are mostly equivalent.

    Mike

    Saturday, March 23, 2013 11:31 AM
  • The request string that I need to send to the API service is as follow :

    Request:
    <tt>{"loanAmount":"500,000.00","interestRate":"5.25","loanLength":"25","rewardScheme":"flybuys","result":""}</tt>

    And the response I will receive is on the following form :
    Response:
    <tt>{"calculateMortgageRewards":{"loanAmount":"500,000.00","loanLength":"25","interestRate":"5.25","rewardScheme":"flybuys","result":1008.0}}</tt>

    In order to pass the string to the web service I am doing it as follow :

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                request.Method = "POST";
                request.ContentType = "application/json";
                request.ContentLength = REWARDDATAREQUEST.Length;

    1- how shoud I then format properly the REWARDDATAREQUEST for the send

    2 - how should I parse properly the response for the request

    Sample will help
    regards

    serge


    Your knowledge is enhanced by that of others.

    Saturday, March 23, 2013 12:05 PM
  • Sorry if I was not clear.  Here is how to format REWARDDATAREQUEST:

    string REWARDDATA = "\"loanAmount\":\"{0}\",\"interestRate\":\"5.25\",\"loanLength\":\"{1}\",\"rewardScheme\":\"{2}\",\"result\":\"\"";
    string REWARDDATAREQUEST="{" + string.Format(REWARDDATA,"500,000.00","25","flybuys") + "}";
    

    To get the result value (1008.0), I would use Regex, assuming your result is in a string variable "response":

    Regex r = new Regex(@"result.*?\:(\w+)");
    string result = r.Match(response).Groups [1].value;

    That was typed freehand, but I think it is correct...

    Mike

    Saturday, March 23, 2013 4:21 PM
  • Thanks works ok.

    For other web request I have different results that I wold like to catch up using the regEx as you have done but could not understand really how to go as I ma not use to .

    The first result I get ise from following response :

    <tt>{"savingsGoal":"123,123.00","alreadySaved":"12,321.00","fortnightlyDeposit":"5,000.00","goalDate":"","interestRate":"4.00","rwtRate":"33","resultMessageKey":"resultTerm","result1":"10","result2":""}}</tt>

    <tt>From result above I need to catch all result.* string, how to do with regEx ?</tt>

    The second result is as follow:<tt>{"homeLoanBalance":"500,000.00","loanLength":"12","interestRate":"5.9","combinedAccountBalances":"50,000","monthlySavings":"12,321.00","savingsResult":22619.6,"timeSavedResult":"2
    years, 10 months"}}
    </tt>

    how to get the SavingResult and TimeSavedResult string ?

    thnaks for help

    serge


    Your knowledge is enhanced by that of others.

    Sunday, March 24, 2013 8:50 PM
  • It might just be better to split the results on the comma character, then split each part on the colon character.  You might also search for a json parser, as the data you are showing is a json response string.  I suspect, though I have never tried, that there exists something in .Net.

    Mike

    Monday, March 25, 2013 8:48 PM