I'm trying to get the dictionary value of T-map API json result with Python, but it doesn't work

Asked 2 years ago, Updated 2 years ago, 47 views

Hi, how are you?

Using anaconda, T-map API was imported from Python as a request for json, and it was saved in dictionary form. I want two values here, but it's hard to get them. What did you type wrong?

The data I want to import is totalDistance and totalTime. I was wondering if I misunderstood the dictionary structure, so I changed it to a data frame and saved it as Excel.

And then The type is FeatureCollection

feautres에는 {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [14395473.264624588, 4237025.082804387]}, 'properties': {'totalDistance': 6606, 'totalTime': 879, 'totalFare': 0, 'taxiFare': 7400, 'index': 0, 'pointIndex': 0, 'name': '', 'description': '돋질로92번길 을 따라 방면으로 30m 이동', 'nextRoadName': '돋질로92번길', 'turnType': 200, 'pointType': 'S'}}

It comes out like this

I thought it would come out if I typed it like this, but I guess I got it wrong a['FeatureCollection']['totalDistance']

How can I get that price out?

This is the code below

import requests
import logging

logging.basicConfig(format='[%(levelname)s] %(asctime)s - %(message)s', level=logging.INFO, datefmt="%H:%M:%S")

version = 1
callback = "result"
apiUrl = f"https://apis.openapi.sk.com/tmap/routes?version={version}&callback={callback}"

headers = {
    "appKey": "----",
    "Accept": "application/json",
    "Content-Type": "application/json; charset=UTF-8"
}

json = {
    "startX" : "129.3165257",
    "startY" : "35.536378",

    "endX" : "129.3268071",
    "endY" : "35.5265278",
    "reqCoordType" : "WGS84GEO",
    "resCoordType" : "EPSG3857"
    # # "searchOption" : searchOption,
    # # "trafficInfo" : trafficInfochk
}

req = requests.post(apiUrl, headers=headers, json=json)

logging.info(req.json())

python dictionary

2022-09-20 18:03

1 Answers


j = req.json()

for feat in j["features"]:
    logging.info(feat)

feat = j["features"][0]
dist = feat["properties"]["totalDistance"]
time = feat["properties"]["totalTime"]
logging.info(f"totalDistance = {dist}, totalTime = {time}")


2022-09-20 18:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.