본문 바로가기

Flutter-플러터

공공API 사용할 때 한글 깨짐

반응형
문제 해결 목적 

 

 

세광2차아í

현재 강화도 공공 버스앱을 만들고 있는데 해당 버스의 정류장 이름이 한글로 안나옴 

 

 

https://okky.kr/questions/1169440

 

OKKY - api 호출시 xml 한글깨짐현상관련하여 질문드립니다..

https://openapi.gg.go.kr/AbdmAnimalProtect여기 공공데이터에서 api호출로 데이터값을 가져오고싶은데호출하면영어와 숫자는 가져와지는데 한글은 ???물음표로 깨져서 나옵니다.인코딩과정에서 한글이 깨

okky.kr

여기서는 UTF-8 변환 문제 

 

 

문제 해결 방법 

 

utf8. 디코드 하는 코드를 추가 

Future<List<BusStop>> fetchBusStops(String routeId) async {
  final url = Uri.parse(
      'https://apis.data.go.kr/1613000/BusRouteInfoInqireService/getRouteAcctoThrghSttnList?serviceKey=서비스키&_type=json&cityCode=23&routeId=$routeId');

  final response = await http.get(url, headers: {
    'Content-Type': 'application/json; charset=UTF-8',
  });

  if (response.statusCode == 200) {
    final responseData = utf8.decode(response.bodyBytes); // UTF-8로 디코딩
    print('API response data for route $routeId: $responseData'); // API 응답 출력

    try {
      final jsonData = json.decode(responseData);
      if (jsonData['response']['header']['resultCode'] == '00') {
        final items = jsonData['response']['body']['items']['item'];
        print('Parsed JSON items for route $routeId: $items'); // JSON 파싱된 데이터 출력
        return items is List
            ? items.map((item) => BusStop.fromJson(item)).toList()
            : [BusStop.fromJson(items)];
      } else {
        throw Exception('Error: ${jsonData['response']['header']['resultMsg']}');
      }
    } catch (e) {
      print('Failed to parse JSON for route $routeId: $e');
      throw Exception('Failed to parse JSON');
    }
  } else {
    throw Exception('Failed to load data');
  }
}

 

 

 

결과

 

 

 

잘 나온당~~~

반응형