kokoroterada
kokoroterada

Reputation: 37

Can't get the intended data with google place api

Problem

I tried to get the data of the review and the phone number with the following url, but most of the values ​​except the text and rating of the phone number and the review are nil. .. However, if you hit the url directly on the web, you can check the intended json data. https://maps.googleapis.com/maps/api/place/details/json?place_id="PLACE ID"&fields=reviews,formatted_phone_number&key="api key"

Reference says if available. at the end of the description of the missing data. What does this mean? I don't know what it means.

I think there are no grammatical mistakes because there is data that can be obtained. In addition, since the Model generated the data confirmed by hitting the api directly using quicktype, it is hard to think of a typo.

struct Empty: Codable {
    let result: Result
    let status: String?
}

// MARK:-Result
struct Result: Codable {
    let formattedPhoneNumber: String?
    let reviews: [Review]
}

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

jsonData (I'm wondering if I can show the data as it is, so I'm replacing the actual data with x)

{
   "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": "xxxxxxxxx",
            "time": 1234567
         }
      ]
   },
   "status": "OK"
}

Upvotes: 0

Views: 80

Answers (1)

Bilal
Bilal

Reputation: 19156

Server response is in snake case so you need to set the decoder key encoding strategy before you decode the json. Something like this

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

Upvotes: 1

Related Questions