Xamarin Forms - JSON - 获取嵌套值

Xamarin Forms - JSON - Getting Nested Values

很抱歉这个基本问题,请不要喷我。

但是如何从 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);

这是我的 class:

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

我还尝试将反序列化器更改为另一个 class,这样我就可以取出 formatted_address,但似乎没有值存储在该变量中。

Result results = JsonConvert.DeserializeObject<Result>(response_package);

如有任何建议,我们将不胜感激。

代码基本正确,只需要再次从Root对象中获取address_components即可

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