首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin谷歌地图API Json不转换

Xamarin谷歌地图API Json不转换
EN

Stack Overflow用户
提问于 2016-05-11 22:04:11
回答 1查看 398关注 0票数 0

问题是当我测试这个应用程序的时候

Task.Run(() => this.LoadDataAsync(url)).Wait();

我得到一个错误Newtonsoft不能反序列化当前的JSON对象。有人能明白为什么吗?

我使用http://json2csharp.com/为JSON创建了类。该应用程序正常工作,并在另一次通话中返回Google上的lat液化天然气。

代码语言:javascript
复制
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:

代码语言:javascript
复制
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呼叫:

代码语言:javascript
复制
    public async void ApiCallCall ()
    {
        Task.Run(() => this.LoadDataAsync(url)).Wait();         
    }
EN

回答 1

Stack Overflow用户

发布于 2017-02-19 01:16:02

摆脱包装在所有其他类中的GoogleApiResponseJson类,并反序列化RootObject(而不是列表),如下所示。

代码语言:javascript
复制
var response = JsonConvert.DeserializeObject<RootObject>(responseJsonString);

RootObject有一个结果列表,这将是您所要寻找的大部分结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37174036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档