Click here to Skip to main content
14,936,800 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
{
  "endpoint": "live", 
  "quotes": [
    {
      "ask": 1.21153, 
      "base_currency": "EUR", 
      "bid": 1.21153, 
      "mid": 1.21153, 
      "quote_currency": "USD"
    }, 
    {
      "ask": 1.39483, 
      "base_currency": "GBP", 
      "bid": 1.39481, 
      "mid": 1.39482, 
      "quote_currency": "USD"
    }
  ], 
  "requested_time": "Thu, 29 Apr 2021 16:19:11 GMT", 
  "timestamp": 1619713151
}



this is my Result i want to get the output from API and deserialized it i tried it but got the
Input string was not in a correct format: c#
Error can you help me on that

What I have tried:

 private async void getCurrentPrize()
        {
            HttpClient client = new HttpClient();
            //HttpResponseMessage response = await client.GetAsync("https://financialmodelingprep.com/api/v3/fx/EURUSD,USDINR,USDCAD,XAUUSD,XAGUSD,GBPUSD,EURJPY,AUDUSD?apikey=xxxxxxxxxxxxxxxxxxxx");
            HttpResponseMessage response = await client.GetAsync("https://marketdata.tradermade.com/api/v1/live?currency=EURUSD,GBPUSD&api_key=_xxxxxxxxxxxxx");
            response.EnsureSuccessStatusCode();
            var responseBody = await response.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject<CurrencyDetails>(responseBody);
            CurrencyDetails deserialized = new CurrencyDetails();
            foreach (var item in result.quotes)
{
   //my logic here
}


public class CurrencyDetails
   {


       public string endpoint { get; set; }

       public List<quotes> quotes { get; set; }

       public string requested_time { get; set; }

       public long timestamp { get; set; }

   }


public class quotes
   {
       public double ask { get; set; }
       public double bid { get; set; }
       public string base_currency { get; set; }
       public double mid { get; set; }
       public string quote_currency { get; set; }
   }


this is my code can you help me on this
Posted
Updated 9-May-21 9:42am
Comments
Gerry Schmitz 29-Apr-21 17:31pm
   
An observation: why are your properties for the quotes class not in the same order as the JSON text?

(To the troll: I have no idea if it makes a difference to serialization).
Richard Deeming 30-Apr-21 12:34pm
   
Your code works fine for me with the sample input. You'll need to debug your code to find out what the problem is. There's not enough information here for anyone to solve the problem for you.

1 solution

There is a online JSON to C# class converter. You input the json string and it pops out a simple c# class definition.
Convert JSON to C# Classes Online - Json2CSharp Toolkit[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900