주제/flutter
[flutter] json 데이터 encode, decode 처리
dev-nam
2022. 9. 27. 21:41
플러터에서 json 데이터 encode, decode 처리하는 방법
이번에는 플러터에서 json 데이터를 인코딩, 디코딩하는 코드를 구현해볼게요. 너무 간단한데 가끔 코드를 깜빡하는 절 위해... 또는 저와 비슷한 누군가를 위해 남겨볼게요.
dart:convert 라이브러리 추가
import 'dart:convert';
json encoding
void main() {
final items = [
{'id': 1, 'title': 'Item 1'},
{'id': 2, 'title': 'Item 2'},
];
print(json.encode(items));
}
인코딩 출력 결과
>>> [{'id':1,'title':'Item 1'},{'id':2,'title':'Item 2'}]
json decoding
void main() {
const String data = '[{'id':1,'title':'Item 1'},{'id':2,'title':'Item 2'}]';
final items = json.decode(data);
print(items.runtimeType);
print(items[0]['title']);
}
디코딩 출력 결과
>>> List<dynamic>
>>> Item 1
간단한 코드지만 도움되었길 바래요.
글 읽어주셔서 감사하고, 즐거운 플러터 코딩하세요 😄
반응형