SwitUI从API地理编码Google提取JSON结果

问题描述 投票:0回答:1

我必须从地址解析Goodle API中获取JSON结果。

JSON结果:

{
  "plus_code" : {
  "compound_code" : "2V6X+23 Ostritz, Germania",
  "global_code" : "9F3P2V6X+23"
  },
  "results" : [
  {
     "address_components" : [
            {
              "long_name" : "Unnamed Road",
              "short_name" : "Unnamed Road",
              "types" : [ "route" ]
            },
            {
              "long_name" : "Ostritz",
              "short_name" : "Ostritz",
              "types" : [ "locality", "political" ]
            },
            {
              "long_name" : "Görlitz",
              "short_name" : "GR",
              "types" : [ "administrative_area_level_3", "political" ]
            },
            {
              "long_name" : "Dresden",
              "short_name" : "DD",
              "types" : [ "administrative_area_level_2", "political" ]
            }
     ],
     "formatted_address" : "Unnamed Road, 02899 Ostritz, Germania",
     "geometry" : {
            "bounds" : {
                  "northeast" : {
                    "lat" : 51.0103607,
                    "lng" : 14.898013
                    },
                  "southwest" : {
                    "lat" : 51.0093767,
                    "lng" : 14.8962029
                    }
             },
            "location" : {
                 "lat" : 51.0097477,
                 "lng" : 14.8972969
                 },
                 "location_type" : "GEOMETRIC_CENTER",
                 "viewport" : {
                    "northeast" : {
                        "lat" : 51.01121768029149,
                        "lng" : 14.8984569302915
                       },
                    "southwest" : {
                        "lat" : 51.0085197197085,
                        "lng" : 14.8957589697085
                       }
                   }
            },
           "place_id" : "ChIJ6egabgkhCUcRznhL6gAPj_w",
           "types" : [ "route" ]
     },
     {
     "address_components" : [
        {
           "long_name" : "Ostritz",
           "short_name" : "Ostritz",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Circondario di Görlitz",
           "short_name" : "GR",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Dresda",
           "short_name" : "DD",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Sassonia",
           "short_name" : "SN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Germania",
           "short_name" : "DE",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Ostritz, Germania",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 51.06239919999999,
              "lng" : 14.964849
           },
           "southwest" : {
              "lat" : 50.9721983,
              "lng" : 14.87143
           }
        },
        "location" : {
           "lat" : 51.01573639999999,
           "lng" : 14.9314311
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 51.06239919999999,
              "lng" : 14.964849
           },
           "southwest" : {
              "lat" : 50.9721983,
              "lng" : 14.87143
           }
        }
     },
     "place_id" : "ChIJKfHi7N8gCUcR2V02pmCO3mU",
     "types" : [ "locality", "political" ]
  },
  ........ 
  ........
 ],
 "status" : "OK"
}

型号:

import Foundation

struct Postgeo: Codable {
   let plus_code: Plus_code
   let results: [Results]
   let status: String
}

struct Results: Codable {
   let address_components: [Address_components]
   let formatted_address: String
   let geometry: Geometry
   let place_id: String
   let plus_code: Plus_coderes
   let types: [String]
 }

 struct Geometry: Codable {
   let location: Location
   let location_type: String
   let viewport: Viewport
 }

 struct Plus_code: Codable {
   let compound_code, global_code: String
 }

 struct Address_components: Codable {
   let long_name, short_name: String
   let types: [String]
 }

 struct Location: Codable {
   let lat, lng: Double
 }

 struct Northeast: Codable {
   let lat, lng: Double
 }

APIManage:

import Foundation
import Combine

class APIManager: ObservableObject {
   let objectWillChange = PassthroughSubject<Void, Never>()


   var postgeos = Postgeo.self {
      willSet {
          objectWillChange.send()
      }
   }

   init() {
      guard let url = URL(string: "<url>") else {
        fatalError("Invalid URL")
      }

      URLSession.shared.dataTask(with: url) { data, response, error in

          guard let json = data else { return }

          let postgeo = try? JSONDecoder().decode(Postgeo.self, from: json)

          print(postgeo)

          DispatchQueue.main.async {
              self.postgeos = postgeo
          }
          print("API values fetched Successfully")
      }.resume()
  }
}

调试时出现错误:

无法分配类型为“ Postgeo的值?”键入“ Postgeo.Type”

在行self.postgeos = postgeo

如果我对此行发表评论,则我得到print(postgeo)的正确结果>

我已为swiftui改编了一个Json提取代码,该代码可与以'['开头的Json很好地工作,但来自地理编码Google api的结果json以'{'开头。

如何解决此错误?

谢谢。

我必须从地址解析Goodle API获取JSON结果。 JSON结果:{“ plus_code”:{“ compound_code”:“ 2V6X + 23 Ostritz,Germania”,“ global_code”:“ 9F3P2V6X + 23”},“结果”:[{“ ...

json swift fetch xcode11.2
1个回答
1
投票
如果print(postgeo)显示正确的结果,则postgeos会引起问题。
© www.soinside.com 2019 - 2024. All rights reserved.