์นดํ
๊ณ ๋ฆฌ ์์
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
}