【问题标题】:Xamarin Forms - JSON - Getting Nested ValuesXamarin 表单 - JSON - 获取嵌套值
【发布时间】:2020-12-14 21:48:11
【问题描述】:

对不起,这个基本问题,请不要喷我。

但是如何从 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# ios json xamarin.forms google-maps-api-3


    【解决方案1】:

    代码基本正确,只需再次从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; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      相关资源
      最近更新 更多