[Java] Json ๋‹ค๋ฃจ๊ธฐ

์ž๋ฐ”์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ json์ด ์ œ๊ณต๋˜์ง€ ์•Š๋Š”๋‹ค.
๋•Œ๋ฌธ์— ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋•ก๊ฒจ์จ์•ผ ํ•˜๋Š”๋ฐ, ๊ฐœ์ค‘์—์„œ ์ œ์ผ ๋‚˜์€๊ฒŒ ๊ตฌ๊ธ€์—์„œ ๋งŒ๋“  Gson์ด๋ผ๊ณ  ํ•œ๋‹ค.

jar ๋‹ค์šด๋กœ๋“œ
https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5

๋ ˆํผ๋Ÿฐ์Šค
https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.5

์—ฌํŠผ, ์ด๊ฑธ ๋•ก๊ฒจ์จ๋„ Json์„ ์“ฐ๋Š”๊ฒŒ ๊ฝค ๊นŒ๋‹ค๋กœ์› ๋‹ค.

์•„๋ž˜๋Š” python ์„œ๋ฒ„์—์„œ ๋ณด๋‚ด๋Š” json์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ ์†Œ์Šค๋‹ค.
์•ž์„œ ํฌ์ŠคํŒ…ํ–ˆ๋˜ Python Json ๊ธ€์—์„œ ์ด์–ด์ง„๋‹ค.

import java.net.*;
import java.io.*;
import com.google.gson.*;
import com.google.gson.stream.*;

//๋ณ€ํ™˜์„ ์œ„ํ•œ ์‹ค์ œ ํƒ€์ž…. 
//Js, Python๊ณผ ๋‹ฌ๋ฆฌ ๋™์  ํƒ€์ž…์ด ์ง€์›๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•จ.
class Person
{
    public String name;
    public String age;
    public String[] family;
}

public class Main
{
    final static String server_address = "์„œ๋ฒ„ ์•„์ดํ”ผ";
    final static int port = 12345;

    public static void main(String[] args)
    {
        try
        {
            //์—ฐ๊ฒฐ ํ›„ ์ฝ์–ด์˜ด
            var socket = new Socket(server_address, port);
            var socket_reader = socket.getInputStream();
            var buffer = new byte[1000];
            socket_reader.read(buffer, 0, 1000);
            var json_string = new String(buffer, "UTF-8");

            System.out.println(json_string); //ํ™•์ธ

            var json_reader = new JsonReader(new StringReader(json_string)); //String์œผ๋กœ ๋ฐ”๋กœ ๋ณด๋‚ด๋Š”๊ฒƒ๋„ ์žˆ๋Š”๋ฐ ๊ทธ๊ฑฐ ์“ฐ๋ฉด ์—๋Ÿฌ๋‚จ. ๋ฎ์–ด์ค˜์•ผ ํ•จ.
            Person result = new Gson().fromJson(json_reader, Person.class); //Person ํƒ€์ž…์œผ๋กœ ์‹ค์ฒดํ™”. ๋ณ€์ˆ˜์— ํƒ€์ž… ๋ช…์‹œ ํ•„์š”

            System.out.println(result.family[1]); //ํ™•์ธ

            socket.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

์—ฌ๊ธฐ์„œ๋Š” Person์ด๋ผ๋Š” ๋ช…ํ™•ํ•œ ํƒ€์ž…์œผ๋กœ ์‹ค์ฒดํ™”๋ฅผ ํ–ˆ๋Š”๋ฐ, Json์˜ ๋™์  ํƒ€์ž…์— ๋งž๊ฒŒ ์ถ”์ƒํ™”๋œ ํƒ€์ž…์ธ JsonObject๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค.
new JsonParser().parse(json๋ฌธ์ž์—ด).getAsJsonObject()...
๋Œ€์‹  ์ด๋†ˆ์€ ๋ช…ํ™•ํ•œ ํƒ€์ž…์„ ์•Œ์•„๋‚ด์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์š”์†Œ๋ฅผ getํ• ๋•Œ๋งˆ๋‹ค ํƒ€์ž… ๋ช…์‹œ๋ฅผ ํ•ด์ค˜์•ผํ•œ๋‹ค.

์ด JsonObject๋ผ๋Š” ๋†ˆ์„ ํ•œ๋ฒˆ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ƒ์„ฑ์„ ํ•ด๋ณด์ž

import com.google.gson.*;
import com.google.gson.stream.*;

public class Main
{
    public static void main(String[] args)
    {
        try
        {
            var json = new JsonObject();

            json.addProperty("name", "๊ฒฝ์ง");
            json.addProperty("age", 65536);
            json.addProperty("nation","korea");

            System.out.println(json.toString());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}


JsonObject์— ๋‹ค๋ฅธ JsonObject๋ฅผ ๋ผ์›Œ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๊ฒƒ๋งŒ ๋ฉ”์„œ๋“œ ์ด๋ฆ„์ด ๋‹ค๋ฅด๋‹ค. add.

import com.google.gson.*;
import com.google.gson.stream.*;

public class Main
{
    public static void main(String[] args)
    {
        try
        {
            var json = new JsonObject();

            json.addProperty("name", "๊ฒฝ์ง");
            json.addProperty("age", 65536);
            json.addProperty("nation","korea");

            var child = new JsonObject();
            child.addProperty("์œผ์•„์•„", "์•Š๋˜");
            child.addProperty("๋‚ด ๋’ค๋ฅผ", "๋ฐ”๋ฆฌ์Šคํƒ€");

            //์ด์–ด๋ถ™์ด๊ธฐO
            json.add("child",child);

            System.out.println(json.toString());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}


์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ์•ž์„œ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ๊ณ„์† ํƒ€์ž… ๋ช…์‹œ๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ