I can't get the intended data in google place api

Asked 1 years ago, Updated 1 years ago, 103 views

I tried to get the review and phone number data in url below, but most of the values except text, rating for phone numbers and reviews are nil.However, if you hit url directly on the web, you can see the data of json intended.
https://maps.googleapis.com/maps/api/place/details/json?place_id="PLACE ID"&fields=reviews, formatted_phone_number&key="api key"
The reference says if available. at the end of the missing data description, but I don't know what this means.

I think there are no grammatical errors because there are some data that have been taken.
Also, it is difficult to think of a typo because the Model generated the confirmed data by tapping api directly using quicktype.

 structure Empty: Codable {
    let result —Result
    let status —String?
}

// MARK: - Result
structureResult:Codable {
    let formattedPhoneNumber—String?
    let reviews —Review
}

// MARK: - Review
struct Review:Codable {
    let authorName —String?
    let authorurl —String?
    let language —String?
    let profilePhotourl: String?
    Lettering—Int?
    let relativeTimeDescription, text: String?
    let time —Int?
}

jsonData (I'm replacing the actual data with x because I'm not sure if I should show it properly)

{
   "html_attributions": [ ],
   "result": {
      "formatted_phone_number": "xx-xxxx-xxxx",
      "reviews": [
         {
            "author_name": "xxxx",
            "author_url": "https://www.google.com/maps/xxxxx/xxxxxxx/reviews",
            "language": "xx",
            "profile_photo_url": "https://xxxxx.com/-xxxxxxx/xxxxxx/xxxxxx/xxxxxxx/xxxxxx/photo.jpg",
            "rating"—4,
            "relative_time_description": "x months ago",
            "text": "xxxxxxxxxxxxxx.",
            "time"—1234567
         },
         {
            "author_name": "xxxxxxxx",
            "author_url": "https://www.google.com/maps/contrib/xxxxxxxx/reviews",
            "language": "xx",
            "profile_photo_url": "https://lh6.ggpht.com/-xxxxxxx/xxxxxxxx/xxxxxxxxx/xxxxxxxx/xxxxxxx/photo.jpg",
            "rating"—5,
            "relative_time_description": "x weeks ago",
            "text": "xxxxxxxx",
            "time"—1234567
         }
      ]
   },
   "status": "OK"
}

swift google-maps google-cloud google-api

2022-09-30 16:24

1 Answers

Thank you for presenting jsonData.I couldn't get rid of the possibility that the key could be rewritten with some settings or options until you gave me the actual contents, but it didn't seem like that either.

"Most of the values except text, rating are nil", but if you use your current code, have you obtained the following items: language, rating, text, and time?"

This is

ReviewIf the JSON has exactly the same key as the property name of the class, data will be obtained.

indicates that

On the other hand, regarding the item with the missing data, there is no key item in JSON such as "authorName" required to retrieve authorName.

Neither "authorurl" nor "profilePhotourl" nor "relativeTimeDescription" exist during JSON.So,

Of course I can't get those items

That's what it means.

Match the property name of the model exactly to the key name of the JSON

That's one way.

structure PlacesAPIResponse:Codable{
    let result —Result
    let status —String?
}

// MARK: - Result
structureResult:Codable {
    let formatted_phone_number —String?
    let reviews —Review
}

// MARK: - Review
struct Review:Codable {
    let author_name —String?
    let author_url —String?
    let language —String?
    let profile_photo_url —String?
    Lettering—Int?
    let relative_time_description, text: String?
    let time —Int?
}

(Empty I'm rewriting it because I don't know the name of the model.)

We could not find an option to print this format of Swift code on the quicktype site you introduced.You'll have to look for other similar sites or touch them yourself.

However, Swift does not use the Snake Case identifier in principle, so the code does not look like Swift.You may not like this method very much.

CodingKeys to specify the correspondence between the property name and the key name on the JSON side

There is also a way to say

import Foundation

// MARK: - PlaceAPIResponse
structurePlaceAPIResponse:Codable {
    let result —Result?
    let status —String?
}

// MARK: - Result
structureResult:Codable {
    let formattedPhoneNumber—String?
    let reviews —Review?

    enum codingKey: String, codingKey {
        case formattedPhoneNumber = "formatted_phone_number"
        case reviews
    }
}

// MARK: - Review
struct Review:Codable {
    let authorName —String?
    let authorURL: String?
    let language —String?
    let profilePhotoURL: String?
    Lettering—Int?
    let relativeTimeDescription, text: String?
    let time —Int?

    enum codingKey: String, codingKey {
        case authorName = "author_name"
        case authorURL="author_url"
        case language
        caseprofilePhotoURL="profile_photo_url"
        case rating
        case relativeTimeDescription="relative_time_description"
        case text, time
    }
}

This code is still generated from the quicktype site with Explicit CodingKey values in Codable types and Make all properties optional turned on.

Incidentally, the auto-generated code translates the URL portion to uppercase, such as authorURL and profilePhotoURL.Did you manually change some of the code used in your question?

In addition, CodingKeys was generated only when this Explicit CodingKey values in Codable types option was specified.In other cases, CodingKeys will be printed.

However, while most of the item names require mechanical conversion from snake case to camel case, CodingKeys may seem rather difficult for some to read.

  • Match the property name of the model exactly as mechanically converted from the JSON key name
  • In
  • JSONDecoder, specify keyDecodingStrategy=.convertFromSnakeCase

There is also a way to say that.During your question, the code for the part that uses JSONDecoder is not shown, so I will write the following in a way that you can try using Playground Xcode.

import Foundation

let jsonText=#""
{
   "html_attributions": [ ],
   "result": {
      "formatted_phone_number": "xx-xxxx-xxxx",
      "reviews": [
         {
            "author_name": "xxxx",
            "author_url": "https://www.google.com/maps/xxxxx/xxxxxxx/reviews",
            "language": "xx",
            "profile_photo_url": "https://xxxxx.com/-xxxxxxx/xxxxxx/xxxxxx/xxxxxxx/xxxxxx/photo.jpg",
            "rating"—4,
            "relative_time_description": "x months ago",
            "text": "xxxxxxxxxxxxxx.",
            "time"—1234567
         },
         {
            "author_name": "xxxxxxxx",
            "author_url": "https://www.google.com/maps/contrib/xxxxxxxx/reviews",
            "language": "xx",
            "profile_photo_url": "https://lh6.ggpht.com/-xxxxxxx/xxxxxxxx/xxxxxxxxx/xxxxxxxx/xxxxxxx/photo.jpg",
            "rating"—5,
            "relative_time_description": "x weeks ago",
            "text": "xxxxxxxx",
            "time"—1234567
         }
      ]
   },
   "status": "OK"
}
"""#

Let jsonData=jsonText.data(using:.utf8)!

structurePlacesAPIResponse:Codable {
    let result —Result
    let status —String?
}

// MARK: - Result
structureResult:Codable {
    let formattedPhoneNumber—String?
    let reviews —Review
}

// MARK: - Review
struct Review:Codable {
    let authorName —String?
    let authorUrl: String?//<- `authorurl` or `authorURL` is not allowed
    let language —String?
    letprofilePhotoUrl: String?//<- Same as above
    Lettering—Int?
    let relativeTimeDescription—String?
    let text —String?
    let time —Int?
}

do{
    let decoder = JSONDecoder()
    decoder.keyDecodingStratey=.convertFromSnakeCase
    let response = try decoder.decode (PlacesAPIResponse.self, from:jsonData)
    print(response)
} catch{
    print(error)
}

Note some of the property names in Review that specify authorUrl and profilePhotoUrl, and .convertFromSnakeCase.

Solution 2 has more freedom because it is free to match property names to JSON key names without being bound by conversion rules that JSONdecoder can understand.

However, many people may prefer Solution 3 if the key name can be converted into a camel case beautifully like this API.

Try one of these methods to suit your coding style.


2022-09-30 16:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.