首页 > 解决方案 > 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);

任何意见,将不胜感激。

标签: c#iosjsonxamarin.formsgoogle-maps-api-3

解决方案


代码基本上是正确的,你只需要再次address_componentsRoot对象中获取。

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; }
}

推荐阅读