我目前正试图通过反序列化将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
});
}
我使用的模型谷歌的地方(工作的地方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;}
}
}
我遇到的问题是,当我从细节调用中反序列化json字符串时,"结果"属性显示为null。
我将非常感谢任何帮助或建议来解决这个问题。
实际上,我使用http://json2csharp.com/解决了这个问题,以防其他人有这种问题。不确定"为什么",但我怀疑它要么与我的命名约定有关,要么与我没有包括结果的每个方面有关。