我目前正在尝试通过反序列化将JSON从google places details api调用转换为c#对象。我已成功完成此操作,使用相同的流程进行Google地理编码调用和Google地方调用。
这些是我的谷歌地方,并提供详细信息:
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;}
}
}
我用于详细信息(结果为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;}
}
}
我遇到的问题是,当我从详细信息调用中反序列化json字符串时,“Results”属性显示为null。
我非常感谢您解决此问题的任何帮助或建议。
答案 0 :(得分:1)
实际上,我使用http://json2csharp.com/修复了此问题,以防其他人遇到此类问题。对于&#34;为什么&#34;并不是正面的,但我怀疑它必须与我的命名惯例有关,或者我不包括结果的每个方面。