Javaの「Jackson」はデファクト・スタンダードなJSONをパースするライブラリです。JavaのオブジェクトをJSONに変換したり、JSONの文字列を受け取ってJavaのオブジェクトに変換できます。
    
    
    
    
👽 インストール
Gradleを使っている場合はbuild.gradleにdependenciesを追加します。gradle clean compileJava --debugを実行してコンパイルがとおれば成功です。
apply plugin: 'java'
  sourceCompatibility = '1.8' targetCompatibility = '1.8'
  repositories {     mavenCentral() }
  // 以下を追加 dependencies {     compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.8.1' }
   | 
 
pom.xmlを使っている場合はこちらを追加します。
<dependency>   <groupId>com.fasterxml.jackson.coregroupId>   <artifactId>jackson-databindartifactId>   <version>2.8.8version> dependency> <dependency>   <groupId>com.fasterxml.jackson.coregroupId>   <artifactId>jackson-annotationsartifactId>   <version>2.8.8version> dependency>
   | 
 
🐞 Javaオブジェクト => JSON
JavaのオブジェクトからJSON形式でデータを出力します。
public class Sample {     public int id;     public String name;
           private static ObjectMapper objectMapper = new ObjectMapper();
      Sample() {                  objectMapper.enable(SerializationFeature.INDENT_OUTPUT);     }
           public String getIdName() {         return String.valueOf(id) + name;     }
      public static void main(String[] args) throws JsonProcessingException {         Sample hoge = new Sample();         hoge.id = 1;         hoge.name = "hoge";
                   String json = objectMapper.writeValueAsString(hoge);
          System.out.println(json);     } }
  | 
 
出力は次のようになります。
{   "id" : 1,   "name" : "hoge",   "idName" : "1hoge" }
  | 
 
ちなみにJacksonのObjectMapperはスレッドセーフですので、static fieldにしてインスタンスを使いまわすことが推奨されています。
java - Should I declare Jackson’s ObjectMapper as a static field?
 😎 基本的な使い方
JSON文字列 => Javaオブジェクト
JSON形式のデータ(文字列)からJavaのオブジェクトを生成します。
public class Sample {     public int id;     public String name;
      private static ObjectMapper objectMapper = new ObjectMapper();
      @Override     public String toString() { return "id = " + id + ", name = " + name; }
      public static void main(String[] args) throws IOException {         String jsonStr = "{\"id\":1,\"name\":\"hoge\"}";
                   Sample result = objectMapper.readValue(jsonStr, Sample.class);
          System.out.println(result);     } }
  | 
 
出力は次のようになります。
List/Mapを含むJSON => Javaオブジェクト
ListやMapなどの型引数をもつクラスをJavaオブジェクトに変換する場合はTypeReferenceを利用します。TypeReferenceによって型安全にでぃシリアライズできます。
public class Sample {     public int id;     public String name;
      private static ObjectMapper objectMapper = new ObjectMapper();
      @Override     public String toString() {         return "id = " + id + ", name = " + name;     }
      public static void main(String[] args) throws IOException {         String jsonStr = "[{\"id\":1, \"name\":\"hoge\"}, {\"id\":2, \"name\":\"fuga\"}]";
                   List result = objectMapper.readValue(jsonStr, new TypeReference>() {          });
          System.out.println(result);     } }
  | 
 
出力は次のとおりです。
[id = 1, name = hoge, id = 2, name = fuga]
   | 
 
 🐰 アノテーション
アノテーションを使うことで簡単に出力するJSONを加工できます。
JSONのプロパティ名を変更:@JsonProperty
@JsonPropertyは出力するプロパティの名前を変更するアノテーションです。
public class Sample {     public int id;
      @JsonIgnore     public String name;
      private static ObjectMapper objectMapper = new ObjectMapper();
      public static void main(String[] args) throws IOException {         Sample hoge = new Sample();         hoge.id = 1;         hoge.name = "hoge";
                   String json = objectMapper.writeValueAsString(hoge);
          System.out.println(json);     } }
  | 
 
出力したJSONではフィールド名がfieldNameに変更されました。
{"id":1,"fieldName":"hoge"}
  | 
 
特定のフィールドを対象外にする:@JsonIgnore
@JsonIgnoreは特定のフィールドをJSONの変換対象外にするアノテーションです。
public class Sample {     public int id;
      @JsonIgnore     public String name;
      private static ObjectMapper objectMapper = new ObjectMapper();
      public static void main(String[] args) throws IOException {         Sample hoge = new Sample();         hoge.id = 1;         hoge.name = "hoge";
                   String json = objectMapper.writeValueAsString(hoge);
          System.out.println(json);     } }
  | 
 
出力したJSONではnameフィールドが出力されませんでした。
対象外のフィールドをまとめて設定:@JsonIgnoreProperties
@JsonIgnorePropertiesをクラスに設定することで、複数のフィールドをまとめてJSON出力の対象外にできます。
@JsonIgnoreProperties({ "id", "description" }) public class Sample {     public int id;     public String name;     public String description;
      private static ObjectMapper objectMapper = new ObjectMapper();
      public static void main(String[] args) throws IOException {         Sample hoge = new Sample();         hoge.id = 1;         hoge.name = "hoge";         hoge.description = "hoge description";
                   String json = objectMapper.writeValueAsString(hoge);
          System.out.println(json);     } }
  | 
 
出力したJSONではid、descriptionフィールドが出力されませんでした。
JSONの未定義フィールドを無視する
デフォルトの挙動では、パースするJSONに未定義のフィールドがある場合、エラーとなります。それを回避するには@JsonIgnorePropertiesアノテーションにignoreUnkown = trueを設定します。
 @JsonIgnoreProperties(ignoreUnknown = true) public class Sample {     public int id;     public String name;
      private static ObjectMapper objectMapper = new ObjectMapper();
      @Override     public String toString() { return "id = " + id + ", name = " + name; }
      public static void main(String[] args) throws IOException {         String jsonStr = "{\"id\":1,\"name\":\"hoge\", \"fuga\":\"fugafuga\"}";
                   Sample result = objectMapper.readValue(jsonStr, Sample.class);
          System.out.println(result);     } }
 
  | 
 
コンストラクタの指定:@JsonCreator
@JsonCreatorでJSON文字列からJavaオブジェクトへの変換を行うコンストラクタを指定できます。
public class Sample {     public int id;     public String name;
      private static ObjectMapper objectMapper = new ObjectMapper();
      @Override     public String toString() { return "id = " + id + ", name = " + name; }
           @JsonCreator     private Sample(@JsonProperty("id") int id, @JsonProperty("name") String name) {         this.id = id;         this.name = name + String.valueOf(id);     }
      public static void main(String[] args) throws IOException {         String jsonStr = "{\"id\":1,\"name\":\"hoge\"}";
                   Sample result = objectMapper.readValue(jsonStr, Sample.class);
          System.out.println(result);     } }
  | 
 
出力は次のようになり、指定したコンストラクタが使われていることがわかります。
コンストラクタの指定:@JsonValue
@JsonValueでJavaオブジェクトからJSONに変換するメソッドを指定できます。
public class Sample {     public int id;     public String name;     public String description;
      private static ObjectMapper objectMapper = new ObjectMapper();
      @JsonValue     public Map toJson() {         Map map = new LinkedHashMap();         map.put("id", String.valueOf(id));         map.put("description", description);         return map;     }
      public static void main(String[] args) throws IOException {         Sample hoge = new Sample();         hoge.id = 1;         hoge.name = "hoge";         hoge.description = "hoge description";
                   String json = objectMapper.writeValueAsString(hoge);
          System.out.println(json);     } }
  | 
 
出力は次のようになります。
{"id":"1","description":"hoge description"}
  | 
 
🚌 参考リンク
    
    
    
    
    
    
    🖥 VULTRおすすめ
    
      「VULTR」はVPSサーバのサービスです。日本にリージョンがあり、最安は512MBで2.5ドル/月($0.004/時間)で借りることができます。4GBメモリでも月20ドルです。
      最近はVULTRのヘビーユーザーになので、「ここ」から会員登録してもらえるとサービス開発が捗ります!