【发布时间】:2016-12-17 03:40:23
【问题描述】:
我收到一个无效的转换异常,表明指定的转换无效。在这一行:
RootObject mountain = JsonConvert.DeserializeObject<RootObject>(json1);
来自documentation 这应该没问题吧?我可以看到控制台输出没问题?
响应:[{“Height_ft”:2999.0,“Height_m”:914.0,“ID”:“c1”, “纬度”:57.588007,“经度”:-5.5233564,“名称”:“Beinn Dearg”, “湿度”:0.81,“积雪”:4.99,“温度”:63.0}]
Spinner spinner = (Spinner)sender;
string urlmountain = "http://removed.azurewebsites.net/api/Mountains?name=";
JsonValue json1 = FetchMountain(urlmountain+string.Format("{0}", spinner.GetItemAtPosition(e.Position)));
//below.................................
RootObject mountain = JsonConvert.DeserializeObject<RootObject>(json1); //this line
string toast = mountain.Name;
Toast.MakeText(this, toast, ToastLength.Long).Show();
private JsonValue FetchMountain(string urlmountain)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(urlmountain));
request.ContentType = "application/json";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = request.GetResponse())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
JsonValue jsonDoc1 = JsonObject.Load(stream);
Console.Out.WriteLine("Response: {0}", jsonDoc1.ToString());
// Return the JSON document:
return jsonDoc1;
}
}
}
public class RootObject
{
public string ID { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public string Name { get; set; }
public double? Height_m { get; set; }
public double? Height_ft { get; set; }
public double? temperature { get; set; }
public double? humidity { get; set; }
public double? snowCover { get; set; }
public override string ToString()
{
return Name;
}
}
【问题讨论】:
-
您能否也将您的
RootObject类定义添加到问题中?一个示例 API 调用也应该有所帮助!