【问题标题】:Converting google places details JSON into a C# Object with JavaScriptDeserializer使用 JavaScriptserializer 将 google 地方详细信息 JSON 转换为 C# 对象
【发布时间】:2015-09-03 20:06:30
【问题描述】:

我目前正在尝试通过反序列化将来自 google 地方详细信息 api 调用的 JSON 转换为 c# 对象。对于谷歌地理编码呼叫和谷歌地方呼叫,我已经使用相同的过程成功地完成了这项工作。

这些是我的谷歌地点,以及地点详情电话:

           string uri = "https://maps.googleapis.com/maps/api/place/radarsearch/json?";

           uri += "key=" + mapkey + "&";
           uri += "location=" + lat.ToString() + "," + lon.ToString() + "&";
           uri += "radius=" + radius.ToString() + "&";
           uri += "types=restaurant";

           string detailUri = "https://maps.googleapis.com/maps/api/place/details/json?";

           string results = client.DownloadString(uri);

           JavaScriptSerializer js = new JavaScriptSerializer();

           PlacesResponse placesresults = js.Deserialize<PlacesResponse>(results);

           for(int i = 0; i < placesresults.Results.Length; i++ )
           {

               detailUri += "placeid=" + placesresults.Results[i].Place_Id + "&key=" + mapkey;

               string details = client.DownloadString(detailUri);                  

               DetailResponse detailresults = js.Deserialize<DetailResponse>(details);                  

                   restaurants.Add(new Restaurant()
                       {
                           Name = detailresults.Results.Name,
                           PlaceID = detailresults.Results.Name,
                           AddressNumber = detailresults.Results.Name,
                           PhoneNumber = detailresults.Results.Name,
                           Rating = detailresults.Results.Name,
                           WebSite = detailresults.Results.Name
                       });

           }

我用于 google 地方的模型(用于地方 ID)是:

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;

   namespace DetroitEatz.Models
   {
       public class PlacesResponse
       {
    public string Status {get;set;}
    public PlacesResults[] Results { get; set; }
   }
   public class PlacesResults
   {
    public PlacesGeometry Geometry { get; set; }
    public string Place_Id { get; set; }

   }
    public class PlacesGeometry
   {
    PlacesLocation Location {get; set;}
   }
    public class PlacesLocation
   {
    public double Lat {get;set;}
    public double Lon {get;set;}
  }

  }

我用于 details (results null) 调用的模型是:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace DetroitEatz.Models
 {
    public class DetailResponse
    {
    public string Status {get;set;}
    public DetailResult Results { get; set; }
    }
    public class DetailResult
    {
    public string Name { get; set; }
    public string Website { get; set; }
    public double Rating { get; set; }
    public string Formatted_Address { get; set; }
    public string Formatted_Phone_Number { get; set; }
    public DetailAddress_Components[] Adress_Components { get; set; }
    }
    public class DetailAddress_Components
    {
    public string[] Types { get; set; }
    public string Long_Name { get; set; }
    public string Short_Name { get; set; }

    }
    public class DetailGeometry
    {
    public DetailLocation Location { get; set; }
 }
public class DetailLocation
{
    public string Lat {get;set;}
    public string Lon {get;set;}
}
}

我遇到的问题是,当我从 details 调用中反序列化 json 字符串时,“Results”属性显示为 null。

对于解决此问题的任何帮助或建议,我将不胜感激。

【问题讨论】:

  • 您能否提供一个您在detailsresults 中收到的样本json
  • 注意到您回答了自己的帖子,我发现 json2csharp.com 网站对于正确使用 JSON 响应非常宝贵。顺便说一句,在您的示例代码中,您如何调用 Google API(您是否有机会使用 WebClient)?

标签: c# json google-api google-places-api


【解决方案1】:

实际上,我使用http://json2csharp.com/ 解决了这个问题,以防其他人遇到此类问题。对“为什么”不肯定,但我怀疑这与我的命名约定有关,或者我没有包括结果的各个方面。

【讨论】:

  • 我在使用 json2csharp.com 时运气不佳,但发现它运行良好:jsonutils.com
猜你喜欢
  • 2016-10-02
  • 2012-05-13
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 2016-02-16
  • 1970-01-01
相关资源
最近更新 更多