2

I am trying to Parse Json in C# . (I am beginner in Json)

WebRequest webRequest = WebRequest.Create(url);
                        var httpWebRequest = (HttpWebRequest)webRequest;
                        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

                        String responseText = "";
                        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                        {
                            responseText = streamReader.ReadToEnd();
                        }

//

This is Output in responseText :

{
  "disclaimer": "Exchange rates provided for informational purposes only and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, no guarantees are made of accuracy, validity, availability, or fitness for any purpose. All usage subject to acceptance of Terms: https://openexchangerates.org/terms/",
  "license": "Data sourced from various providers; resale prohibited; no warranties given of any kind. All usage subject to License Agreement: https://openexchangerates.org/license/",
  "timestamp": 1454497211,
  "base": "EUR",
  "rates": {
    "INR": 74.42686146,
    "USD": 1.0929332
  }
}

To get the base from Json

var obj = JObject.Parse(responseText);
String bcur = (string)obj["base"];
Console.WriteLine("base :"bcur.ToString());


Output : base :EUR 

similarly to get Rates of INR , I wrote

var obj = JObject.Parse(responseText);
    String rte= (string)obj["INR"];
    Console.WriteLine("rate:"rte);

it gives me null.

Can anyone tell me what is wrong in my code. or a better way to get the base and rate of the currency .

3
  • Out of curiosity, why are you using .ToString() when you are writing out to console when you already specified the type of the variable as a string? Commented Feb 3, 2016 at 13:43
  • my bad.. i made a correction .. thanks for pointing out. Commented Feb 3, 2016 at 13:47
  • Updated the code in my answer to reflect this too. You are also missing the concatenation character in your console write lines. There should be a + between your string and your variable. Or alternatively, if you are using .Net 4.6 you can use the string interpolation stuff by prefixing your string with a $. Using one of your examples Console.WriteLine("rate:"rte); would look like Console.WriteLine($"rate:{rte}"); Commented Feb 3, 2016 at 13:51

1 Answer 1

2

INR is not in the base object, so you are looking for the INR property in a place where it doesn't exist. You would have to go about it in a two step fashion in order to access the property in . Modifiying your code above, it would look something like this:

var obj = JObject.Parse(responseText);
JObject rates = (JObject)obj["rates"];
string rte = (string)rates["INR"];
Console.WriteLine("rate:" + rte);
Sign up to request clarification or add additional context in comments.

2 Comments

After your answer i realised that there was rates also a JObject. Thanks
Glad I could help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.