JSON Parsing and Dynamic Types
JSON Parsing and Dynamic Types
[Ko]
- XML 포맷
<key>value</key>
- JSON(JavaScript Object Notation) 포맷
{key:value}
- 웹 서버에서 제공되는 JSON 포맷은 dart의 map format과 유사함.
- JSON Viewer Awesome 을 chrome plugin으로 설치하자.
—> 설치후 weather data api의 result 화면에서 refresh를 누르면 보기편한 구조로 바뀌어져 보임.
아래 주소는 sample data를 얻기위한 api임.
- 일반적으로 웹 서버의 데이터는 JSON 포맷으로 압축되어 있으므로 웹 서버에서 데이터를 내려받으면 dart convert package의 jsonDecode method를 이용해서 data를 unpacking한 후 사용해야 함.
dart가 사용하던 data를 웹 서버로 보낼 때는 jsonEncode를 이용해서 다시 packing해서 보내야 함.
- jsonDecode는 실행 결과를 받을 때까지는 정확인 데이터 타입을 알 수 없기 때문에 dynamic data type을 return 받음. --> 따라서 jsonDecode를 실행한 결과를 저장할 변수는 var를 이용해서 dynamic type variable로 생성함.
- final code(loading_screen.dart)
import 'package:flutter/material.dart';
import 'package:clima/services/location.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
getLocation();
getData();
}
void getLocation() async {
Location location = Location();
await location.getCurrentLocation();
print(location.latitude);
print(location.longitude);
}
void getData() async {
http.Response response = await http.get(
if (response.statusCode == 200) {
String Data = response.body;
var decodedData = jsonDecode(data);
double temperature = decodedData['main']['temp'];
int condition = decodedData['weather'][0]['id'];
String cityName = decodedData['name'];
} else {
print(response.statusCode);
}
}
[En]
- XML format
value </ key>
- JSON (JavaScript Object Notation) format
{key: value}
- JSON format provided by web server is similar to dart's map format.
- Install JSON Viewer Awesome with chrome plugin.
—> After installation, if you click refresh on the result screen of weather data api, it appears to be changed to a convenient structure.
The address below is an api for obtaining sample data.
https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22
- In general, the data of the web server is compressed in JSON format, so if you download the data from the web server, you must use it after unpacking the data using the jsonDecode method of the dart convert package.
When sending data used by dart to a web server, it must be packed and sent again using jsonEncode.
- Since jsonDecode does not know the exact data type until it receives the execution result, it returns a dynamic data type. -> Therefore, the variable to store the result of executing jsonDecode is created as a dynamic type variable using var.
- final code(loading_screen.dart)
import 'package:flutter/material.dart';
import 'package:clima/services/location.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class LoadingScreen extends StatefulWidget {
@override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
getLocation();
getData();
}
void getLocation() async {
Location location = Location();
await location.getCurrentLocation();
print(location.latitude);
print(location.longitude);
}
void getData() async {
http.Response response = await http.get(
if (response.statusCode == 200) {
String Data = response.body;
var decodedData = jsonDecode(data);
double temperature = decodedData['main']['temp'];
int condition = decodedData['weather'][0]['id'];
String cityName = decodedData['name'];
} else {
print(response.statusCode);
}
}