I'm am writing a script at Powershell that is running a C# class, and within the class I am getting a string that represents a JSON (through REST API).
I have verified that the response string from the request is valid, but when I am trying to parse it to JSON with using Newtonsoft.Json; I am getting an error that JObject is not recognized.
My Powershell script:
$referencingassemblies = ("Newtonsoft.Json.dll")
Add-Type -Path "WeatherForecast.cs" -ReferencedAssemblies $referencingassemblies
$basicTestObject = New-Object Forecast.WeatherForecast(...)
$basicTestObject.doForecast()
My C# code:
using Newtonsoft.Json;
...
public string doForecast()
{
var dateTimeOffset = new DateTimeOffset(this.requiredDate);
var unixDateTime = dateTimeOffset.ToUnixTimeSeconds();
string apiCall = "http://api.openweathermap.org/data/2.5/onecall/timemachine?lat=" + this.latitude + "&lon=" + this.longitude + "&dt="+ unixDateTime.ToString() +"&units=metric&appid=" + API_KEY;
// Create a web client.
using (WebClient client = new WebClient())
{
// Get the response string from the URL.
try
{
getObservationResponse(client.DownloadString(apiCall));
}
catch (WebException ex)
{
return ex.ToString();
}
catch (Exception ex)
{
return ex.ToString();
}
}
return "done";
}
private void getObservationResponse(string response)
{
JObject json = JObject.Parse(response);
...
}
And the error is:
The type or namespace name 'Json' could not be found (are you
missing a using directive or an assembly reference?)
I also tried to add using Newtonsoft.Json.Linq; which didn't work.
What am I doing wrong?