c# - Xamarin 表单 - JSON - 获取嵌套值
问题描述
对不起,基本问题,请不要喷我。
但是如何从 JSON 包中获取嵌套值?我能够获得第一个字段,但嵌套值让我感到困惑。
这是json响应:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "27",
"short_name" : "27",
"types" : [ "street_number" ]
},
]
"formatted_address" : "xxxxx",
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"name" : "27 Percy St",
"place_id" : "xxxxx",
"reference" : "xxxxx",
"types" : [ "premise" ],
"url" : "https://maps.google.com/?q=xxxxx&ftid=0x6b12bc036a9c581d:0x661b35b7a051a51",
"utc_offset" : 600,
"vicinity" : "xxxx"
},
"status" : "OK"
我想获得 formatted_address 但我似乎只能获得状态部分。
这是我的代码:
string GooglePlaceAPIKey = "xxxx";
string SelectedPlaceID = googleplace.PlaceID;
string GooglePlaceDetailAPI = "https://maps.googleapis.com/maps/api/place/details/json?key=" + GooglePlaceAPIKey + "&place_id=" + SelectedPlaceID;
Console.WriteLine(GooglePlaceDetailAPI);
var client = new HttpClient();
var uri = GooglePlaceDetailAPI;
var response_status = await client.GetAsync(uri);
var response_package = await response_status.Content.ReadAsStringAsync();
Root results = JsonConvert.DeserializeObject<Root>(response_package);
Console.WriteLine(results.status);
这是我的课:
public class Root
{
public List<Prediction> predictions { get; set; }
public string status { get; set; }
// Google Places Details
public List<object> html_attributions { get; set; }
public Result results { get; set; }
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public string icon { get; set; }
public string name { get; set; }
public string place_id { get; set; }
public string reference { get; set; }
public List<string> types { get; set; }
public string url { get; set; }
public int utc_offset { get; set; }
public string vicinity { get; set; }
}
我还尝试将反序列化器更改为另一个类,以便我可以提取 formatted_address 但该变量中似乎没有存储任何值。
Result results = JsonConvert.DeserializeObject<Result>(response_package);
任何意见,将不胜感激。
解决方案
代码基本上是正确的,你只需要再次address_components
从Root
对象中获取。
Root results = JsonConvert.DeserializeObject<Root >(response_package);
var address_components = results.result.address_components;
该模型:
public class Root
{
public string status { get; set; }
// Google Places Details
public List<object> html_attributions { get; set; }
public Result result { get; set; } //notice here you should use "result" the same as the key in your json string.
}
public class Result
{
public List<AddressComponent> address_components { get; set; }
public string formatted_address { get; set; }
public string icon { get; set; }
public string name { get; set; }
public string place_id { get; set; }
public string reference { get; set; }
public List<string> types { get; set; }
public string url { get; set; }
public int utc_offset { get; set; }
public string vicinity { get; set; }
}
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List<string> types { get; set; }
}
推荐阅读
- javascript - 如何使用 Javascript 根据图片的位置选择和播放声音文件?
- ios - 如何在 Swift 或 Objective-C 中搜索多个多词短语的字符串
- typescript - Typescript(编译时)中是否可以使用不可变的命名参数(具有默认值)?
- c - 针对古老的 C 对象构建
- react-native - 在 Navigation Drawer v5 中的 Navigator.Screen 之间插入元素
- python - 提交表单时恢复到 ?page=1 的干净方法(使用 flask-paginate)
- javascript - Javascript 从缩略图在新窗口中查看图像?
- apache-spark - 如何在 AWS Glue PySpark 中运行并行线程?
- .net - 启动时 WPF FileNotFound
- flutter - 线性百分比指示器中的颤动进度颜色