SuperKid 2020. 3. 19. 15:40

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.

https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22

 - 일반적으로 서버의 데이터는 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(

        'https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22');

 

    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(

        'https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22');

 

    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);

    }

  }