JSON νμ±νλ λ€μν λ°©λ² (Maven)
μλ 3κ°μ§λ₯Ό μ΄μ©ν λ°©λ²μ μμλ³Ό κ²μ΄λ€.
Org.JSON
Google.Gson
Jackson
νμ±ν λ μ¬μ©ν 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"
}
]
}
μ°μ 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 ());
}
}
<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" ));
}
<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 μμ±
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 ());
<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 ());