6

I'm using .NET Core for Linux for a Console Program. Using Http functionality I'm get some information coming from a Webservice. Then I'm trying to cast the result to an object but I'm not able to use JSON.

I read this article but I don't find any example and I don't 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 can I use JSON to convert my variable result to an object like I do usually with c#:

        JObject obj = (JObject)JsonConvert.DeserializeObject(result);

I hope you will be able to help me.

Many thanks,

HEWhoDoesn'tKnow
  • 347
  • 3
  • 14
eldondano
  • 167
  • 2
  • 2
  • 11

1 Answers1

1

You'll simply need some kind of dependency which is available for .NET core which can help you deserialize json.

Newtonsoft.Json is defacto standard and is available in .NET core in order to use it you have to add it into your project.json file

"dependencies" {
...
"Newtonsoft.Json": "10.0.3"
},

The appropriate using statement in your class

using Newtonsoft.Json

you can then deserialize using 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);
    }
}
pijemcolu
  • 2,257
  • 22
  • 36
  • Thanks for your answer. But when I try to add this dependencie I have following error messge: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) NETCoreTest..NETCoreApp,Version=v1.0 And The dependency Newtonsoft.Json >= 10.0.3 could not be resolved – eldondano Jun 29 '17 at 09:36
  • Did you add the dependency in project.json? The error message says there's a problem with it. If so try `$ dotnet restore` in console. Or simply look up how to add nugget package to .net core. – pijemcolu Jun 29 '17 at 09:40
  • How to add a nugget package: http://ardalis.com/how-to-add-a-nuget-package-using-dotnet-add – pijemcolu Jun 29 '17 at 09:50