How can I convert my Result variable to an object using JSONConvert?
I am using .NET Core for Linux for a console program. Using the Http function, I am getting some information coming from the Webservice. Then I try to pipe the result to an object, but I cannot use JSON.
I have read this article but I have not found any example and I do not have access to JavaScriptSerializer
public async void CallApi(Object stateInfo)
{
var client = new HttpClient();
var requestContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("pair", "XETHZEUR"), });
HttpResponseMessage response = await client.PostAsync("https://api.kraken.com/0/public/Trades", requestContent);
HttpContent responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
String result = await reader.ReadToEndAsync();
//Here I would like to do a deserialized of my variable result using JSON (JObject obj = (JObject)JsonConvert.DeserializeObject(result);) But I don't find any JSON object
}
}
EDIT I would like to know how I can use JSON to convert my variable result to an object as usual using C #:
JObject obj = (JObject)JsonConvert.DeserializeObject(result);
Hope you can help me.
Many thanks,
source to share
You just need some kind of dependency available to the .NET core that can help you deserialize json.
Newtonsoft.Json is a defacto standard and is available in the .NET core, to use it you must add it to your project.json file.
"dependencies" {
...
"Newtonsoft.Json": "10.0.3"
},
Relevant usage statement in your class
using Newtonsoft.Json
you can deserialize with JsonConvert.DeserializeObject(json);
public async void CallApi(Object stateInfo)
{
var client = new HttpClient();
var requestContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("pair", "XETHZEUR"), });
HttpResponseMessage response = await client.PostAsync("https://api.kraken.com/0/public/Trades", requestContent);
HttpContent responseContent = response.Content;
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
String result = await reader.ReadToEndAsync();
//Here I would like to do a JSON Convert of my variable result
var yourObject = JsonConvert.DeserializeObject(result);
}
}
source to share