์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

JSON ํŒŒ์‹ฑ ์—ฐ์Šต 2๋‹จ๊ณ„

๋™์œค123 2024. 6. 28. 09:04

๐Ÿ’ก JSON Object์™€ JSON Array์˜ ํƒ€์ž…์„ ๋ฐ˜๋“œ์‹œ ๊ตฌ๋ถ„ํ•˜์ž.

JSON Object (JSON ๊ฐ์ฒด):

  • JSON ๊ฐ์ฒด๋Š” { } ๋กœ ๋‘˜๋Ÿฌ์‹ธ์ธ ํ‚ค-๊ฐ’ ์Œ์˜ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค.
  • ํ‚ค๋Š” ํ•ญ์ƒ ๋ฌธ์ž์—ด์ด๊ณ , ๊ฐ’์€ ๋ฌธ์ž์—ด, ์ˆซ์ž, ๊ฐ์ฒด, ๋ฐฐ์—ด, ๋ถˆ๋ฆฌ์–ธ, ๋˜๋Š” null์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
{
  "name": "ํ™๊ธธ๋™",
  "age": 21,
  "subjects": ["์ˆ˜ํ•™", "๋ฌผ๋ฆฌ", "์ปดํ“จํ„ฐ ๊ณผํ•™"]
}

JSON Array (JSON ๋ฐฐ์—ด):

  • JSON ๋ฐฐ์—ด์€ **[ ]**๋กœ ๋‘˜๋Ÿฌ์‹ธ์ธ ๊ฐ’์˜ ์ˆœ์„œ ์žˆ๋Š” ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.
  • ๋ฐฐ์—ด์˜ ๊ฐ ๊ฐ’์€ ๋ชจ๋“  JSON ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
[
  {
    "name": "ํ™๊ธธ๋™",
    "age": 21,
    "subjects": ["์ˆ˜ํ•™", "๋ฌผ๋ฆฌ", "์ปดํ“จํ„ฐ ๊ณผํ•™"]
  },
  {
    "name": "์ด์ˆœ์‹ ",
    "age": 22,
    "subjects": ["์—ญ์‚ฌ", "๊ตฐ์‚ฌํ•™"]
  }
]

 

๋ฌธ์ž์—ด(String)

์ˆซ์ž(Number)

๋ถˆ๋ฆฌ์–ธ(Boolean)

๊ฐ์ฒด(Object)

๋ฐฐ์—ด(List)

๋„(Null)`

 

https://jsonplaceholder.typicode.com/

 

JSONPlaceholder - Free Fake REST API

{JSON} Placeholder Free fake and reliable API for testing and prototyping. Powered by JSON Server + LowDB. Serving ~3 billion requests each month.

jsonplaceholder.typicode.com

package ch02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class MyHttpAlbumClient {

	public static void main(String[] args) {
		
		// ์ˆœ์ˆ˜ ์ž๋ฐ”์ฝ”๋“œ์—์„œ HTTP ํ†ต์‹  
		// 1. ์„œ๋ฒ„ ์ฃผ์†Œ ๊ฒฝ๋กœ
		// 2. URL ํด๋ž˜์Šค 
		// 3. url.openConnection()  <--- ์ŠคํŠธ๋ฆผ I/O
		
		try {
			URL url = new URL("https://jsonplaceholder.typicode.com/albums/1");
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Content-type", "application/json");
			
			// ์‘๋‹ต ์ฝ”๋“œ ํ™•์ธ
			int responseCode = conn.getResponseCode();
			System.out.println("response code : " + responseCode);
			
			// HTTP ์‘๋‹ต ๋ฉ”์„ธ์ง€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœ [] -- Stream ---  []
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String inputLine;
			StringBuffer buffer = new StringBuffer();
			while( (inputLine = in.readLine()) != null ) {
				buffer.append(inputLine);
			}
			in.close();
			
			System.out.println(buffer.toString());
			System.err.println("-------------------------------");
			// gson lib ํ™œ์šฉ 
			//Gson gson = new Gson();
			Gson gson = new GsonBuilder().setPrettyPrinting().create();
			Album albumDTO =  gson.fromJson(buffer.toString(), Album.class);
			
			System.out.println(albumDTO.getId());
			System.out.println(albumDTO.getUserId());
			System.out.println(albumDTO.getTitle());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	} // end of main 
}

 

package ch02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

/**
 * JSON Array ํ˜•ํƒœ๋ฅผ ํŒŒ์‹ฑ ํ•ด๋ณด์ž. 
 */
public class MyHttpAlbumListClient {

	public static void main(String[] args) {
		
		// ์ˆœ์ˆ˜ ์ž๋ฐ”์ฝ”๋“œ์—์„œ HTTP ํ†ต์‹  
		// 1. ์„œ๋ฒ„ ์ฃผ์†Œ ๊ฒฝ๋กœ
		// 2. URL ํด๋ž˜์Šค 
		// 3. url.openConnection()  <--- ์ŠคํŠธ๋ฆผ I/O
		
		try {
			URL url = new URL("https://jsonplaceholder.typicode.com/albums");
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Content-type", "application/json");
			
			// ์‘๋‹ต ์ฝ”๋“œ ํ™•์ธ
			int responseCode = conn.getResponseCode();
			System.out.println("response code : " + responseCode);
			
			// HTTP ์‘๋‹ต ๋ฉ”์„ธ์ง€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœ [] -- Stream ---  []
			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String inputLine;
			StringBuffer buffer = new StringBuffer();
			while( (inputLine = in.readLine()) != null ) {
				buffer.append(inputLine);
			}
			in.close();
			
			System.out.println(buffer.toString());
			System.err.println("-------------------------------");
			// gson lib ํ™œ์šฉ 
			//Gson gson = new Gson();
			Gson gson = new GsonBuilder().setPrettyPrinting().create();
			// ํ•˜๋‚˜์˜ JSON Object ํ˜•ํƒœ ํŒŒ์‹ฑ 
			// Album albumDTO =  gson.fromJson(buffer.toString(), Album.class);
			
			// [{...},{...},{...}]
			// Gson ์ œ๊ณตํ•˜๋Š” Type ์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
			
			//Json ๋ฐฐ์—ด ํ˜•ํƒœ๋ฅผ ์‰ฝ๊ฒŒ ํŒŒ์‹ฑํ•˜๋Š” ๋ฐฉ๋ฒ• -> TypeToken ์•ˆ์— List<T> ์„ ํ™œ์šฉ ํ•œ๋‹ค. 
			Type albumType = new TypeToken<List<Album>>(){}.getType();
			List<Album>  albumList = gson.fromJson(buffer.toString(), albumType);
			
			System.out.println(albumList.size());
			for(Album a : albumList ) {
				System.out.println(a.toString());
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	} // end of main 
}