Skip to content

Latest commit

Β 

History

History
192 lines (159 loc) Β· 4.6 KB

File metadata and controls

192 lines (159 loc) Β· 4.6 KB

JSON νŒŒμ‹±ν•˜λŠ” λ‹€μ–‘ν•œ 방법 (Maven)

  • μ•„λž˜ 3κ°€μ§€λ₯Ό μ΄μš©ν•œ 방법을 μ•Œμ•„λ³Ό 것이닀.
  1. Org.JSON
  2. Google.Gson
  3. Jackson

Project Structure

resources/sample.json

  • νŒŒμ‹±ν•  λ•Œ μ‚¬μš©ν•  sample.json νŒŒμΌμ„ μƒμ„±ν•œλ‹€.
{
  "uuid": "03adf601-8c53-468e-b423-e29609f5c479",
  "color": {
    "id": "color01",
    "name": "white",
    "hex": "#ffffff"
  },
  "animals": [
    {
      "id": 1,
      "name": "μ˜€μΆ˜μ‹",
      "type": "hamster"
    },
    {
      "id": 2,
      "name": "였좘무",
      "type": "fish"
    }
  ]
}

Main.java

  • μš°μ„  sample.json νŒŒμΌμ„ μ½μ–΄μ„œ String λ³€μˆ˜μ— λ‹΄μ•„λ‘”λ‹€.
public class Main {
    public static void main(String[] args) throws IOException {
    	String jsonString = readFile("/sample.json");
    }

    static String readFile(String fileName) throws IOException {
    	return IOUtils.resourceToString(fileName, Charset.defaultCharset());
    }
}

Org.JSON

pom.xml

<properties>
    <org.json.version>20210307</org.json.version>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>${org.json.version}</version>
    </dependency>
</dependencies>
// JSON
JSONObject jsonObject = new JSONObject(jsonString);

System.out.println(jsonObject.get("uuid"));

// JSON μ•ˆμ˜ JSON Object
JSONObject colorObject = jsonObject.getJSONObject("color");

System.out.println(colorObject.get("hex"));

// JSON μ•ˆμ˜ Array μ•ˆμ˜ JSON Object
JSONArray jsonArray = jsonObject.getJSONArray("animals");
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject animalObject = jsonArray.getJSONObject(i);
    
    System.out.println(animalObject.get("name"));
}

Google.Gson

pom.xml

<properties>
    <google.gson.version>2.8.7</google.gson.version>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${google.gson.version}</version>
    </dependency>
</dependencies>
// JSON
JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();

System.out.println(jsonObject.get("uuid").getAsString());

// JSON μ•ˆμ˜ JSON Object
JsonObject colorObject = jsonObject.getAsJsonObject("color");

System.out.println(colorObject.get("hex").getAsString());

// JSON μ•ˆμ˜ Array μ•ˆμ˜ JSON Object
JsonArray jsonArray = jsonObject.getAsJsonArray("animals");
for (int i = 0; i < jsonArray.size(); i++) {
    JsonObject animalObject = jsonArray.get(i).getAsJsonObject();
    
    System.out.println(animalObject.get("name").getAsString());
}

λ‹€λ₯Έ 방법도 μžˆλ‹€.

  • JSON Object κ΅¬μ‘°λŒ€λ‘œ Java Object 생성

Sample.java

package com.example.jsonparsing;

public class Sample {
    private String uuid;
    private Color color;
    private Animal[] animals;
    
    // ... getter μƒλž΅

    static class Color {
    	private String id;
    	private String name;
    	private String hex;

    	// ... getter μƒλž΅
    }

    static class Animal {
        private Long id;
        private String name;
        private String type;

        // ... getter μƒλž΅
    }
}
// λ˜λ‹€λ₯Έ 방법 : class μ‚¬μš©
Sample sample = new Gson().fromJson(jsonString, Sample.class);

System.out.println(sample.getUuid());
System.out.println(sample.getColor().getHex());
System.out.println(sample.getAnimals()[0].getName());

Jackson

pom.xml

<properties>
    <jackson.databind.version>2.12.4</jackson.databind.version>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.databind.version}</version>
    </dependency>
</dependencies>
  • μœ„μ—μ„œ λ§Œλ“  Sample.java μ΄μš©ν•œλ‹€.
ObjectMapper mapper = new ObjectMapper();

Sample sample = mapper.readValue(jsonString, Sample.class);
System.out.println(sample.getUuid());
System.out.println(sample.getColor().getHex());
System.out.println(sample.getAnimals()[0].getName());