gpt4 book ai didi

c# - 使用 JavaScriptserializer 将 google places details JSON 转换为 C# 对象

转载 作者:行者123 更新时间:2023-11-30 16:52:38 25 4
gpt4 key购买 nike

我目前正在尝试通过反序列化将来自 google places details api 调用的 JSON 转换为 c# 对象。对于 google geocode 调用和 google places 调用,我已经使用相同的过程成功完成了此操作。

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

           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 places(用于 place 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;}
}

}

我用于详细信息(结果为空)调用的模型是:

 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。

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

最佳答案

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

关于c# - 使用 JavaScriptserializer 将 google places details JSON 转换为 C# 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32384546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com