问题是当我测试这个应用程序的时候
Task.Run(() => this.LoadDataAsync(url)).Wait();
我得到一个错误Newtonsoft不能反序列化当前的JSON对象。有人能明白为什么吗?
我使用http://json2csharp.com/为JSON创建了类。该应用程序正常工作,并在另一次通话中返回Google上的lat液化天然气。
string url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&name=cruise&key=MYAPI IS HERE";
List<GoogleApiResponseJson> googleApiResponseJson = new List<GoogleApiResponseJson> ();
public async Task LoadDataAsync(string uri)
{
string responseJsonString = null;
using (var httpClient = new HttpClient())
{
try
{
Task<HttpResponseMessage> getResponse = httpClient.GetAsync(uri);
HttpResponseMessage response = await getResponse;
responseJsonString = await response.Content.ReadAsStringAsync();
googleApiResponseJson = JsonConvert.DeserializeObject<List<GoogleApiResponseJson>>(responseJsonString);
}
catch (Exception ex)
{
throw;
}
}
}然后我让这个类保存JSON:
public class GoogleApiResponseJson
{
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Geometry
{
public Location location { get; set; }
}
public class OpeningHours
{
public bool open_now { get; set; }
public List<object> weekday_text { get; set; }
}
public class Photo
{
public int height { get; set; }
public List<string> html_attributions { get; set; }
public string photo_reference { get; set; }
public int width { get; set; }
}
public class Result
{
public Geometry geometry { get; set; }
public string icon { get; set; }
public string id { get; set; }
public string name { get; set; }
public OpeningHours opening_hours { get; set; }
public List<Photo> photos { get; set; }
public string place_id { get; set; }
public double rating { get; set; }
public string reference { get; set; }
public string scope { get; set; }
public List<string> types { get; set; }
public string vicinity { get; set; }
}
public class RootObject
{
public List<object> html_attributions { get; set; }
public List<Result> results { get; set; }
public string status { get; set; }
}}
OnCreate呼叫:
public async void ApiCallCall ()
{
Task.Run(() => this.LoadDataAsync(url)).Wait();
}发布于 2017-02-19 01:16:02
摆脱包装在所有其他类中的GoogleApiResponseJson类,并反序列化RootObject(而不是列表),如下所示。
var response = JsonConvert.DeserializeObject<RootObject>(responseJsonString);RootObject有一个结果列表,这将是您所要寻找的大部分结果。
https://stackoverflow.com/questions/37174036
复制相似问题