简体   繁体   中英

Argument1: Cannot convert from 'System.IO.Stream' to 'String'

I'm stumped on this, and could use some assistance. I've done web searches, and read documentation for the last several hours, and have had no luck.

I'm getting an error that reads as follows from the "response" overload entry of the "JObject o = JObject.Parse(response);" line.

Argument1: cannot convert from 'System.IO.Stream' to 'string'

static void MyFunction(out string Value1, out string Value2)
{
    HttpClient client = new HttpClient();
    var response = client.GetStreamAsync("My URI").Result;
    JObject o = JObject.Parse(response);
    Value1 = (string)o.SelectToken("PressureReading");
    Value2 = (string)o.SelectToken("PressureTrend");
}

I had this code working using webclient in a console app project. However, since this is a for UWP project, I'm unable to use webclient (and am required to use HttpClient). Also, the JSON string I'm parsing from my REST API is as follows:

{"ID":8,"Site":"EstevanPointCanada","PressureReading":"30.05     ","PressureTrend":"0         "}

What changes do I need to make in order for the above function to compile?

Thanks in advance for any assistance provided.

JObject.Parse takes a string , not a Stream . You're trying to pass it response , which is a Stream .

To fix it, just use HttpClient.GetStringAsync instead, eg

using (HttpClient client = new HttpClient())
{
    var response = client.GetStringAsync("My URI").Result;
    JObject o = JObject.Parse(response);
    Value1 = (string)o.SelectToken("PressureReading");
    Value2 = (string)o.SelectToken("PressureTrend");
}

Note that if you find yourself getting confused by errors like this, it's worth making all the types explicit - if you'd used explicit typing for response instead of var , it would have been very obvious that either you'd expected it to be a string and it wasn't, or that you'd expected it to be a Stream but that JObject.Parse didn't accept a stream...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM