Katazui Katazui - 3 months ago 45
JSON Question

Cannot implicitly convert type 'Newtonsoft.Json.Linq.JToken' to 'string'. An explicit conversion exists (are you missing a cast?)

I have the following code:

WebClient c = new WebClient();
var data = c.DownloadString("https://btc-e.com/api/2/btc_usd/ticker");
//Console.WriteLine(data);
JObject o = JObject.Parse(data);
maskedTextBox11.Text = o["high"];


But it's giving the error in the title.

Answer

You are just required to add o["high"].ToString(); instead of o["high"]; since JObject[] returns a JToken and you are trying to assign maskedTextBox11.Text, which is a string, with it.

If you want the ToString() of the object represented by the token you can do it as the following:

MyType obj = o["high"].ToObject<MyType>();
string s = obj.ToString();