2010年6月22日火曜日

JSONICの使い方

最近は、JSON形式のデータを熱かったアプリを作ろうと考えているので、
JSONICについて調べてみました。

JSONICっていうのは簡単にいうと、JavaでJSON形式のデータを扱うためのライブラリです。
ダウンロードはこちらから↓
JSONIC

使い方はいたって簡単。

  1. // JSONICをインポート  
  2. import net.arnx.jsonic.JSON;  
  3.   
  4. public class JsonicSample {  
  5.     public static void main(String[] args) {  
  6.   
  7.         final JsonicSample jsonicSample = new JsonicSample();  
  8.         jsonicSample.go();  
  9.   
  10.     public void go() {  
  11.         // エンコード(Java → JSON)対象のクラス  
  12.         final Hoge hoge = new Hoge();  
  13.         hoge.setName("sample");  
  14.         hoge.setAge(10);  
  15.   
  16.         // JSON形式にエンコード  
  17.         final String hogeJson = JSON.encode(hoge);  
  18.   
  19.         // {"name":"sample","age":10} が出力されます  
  20.         System.out.println("エンコード結果:" + hogeJson);  
  21.   
  22.   
  23.         final String hogeJson2 = "{\"name\":\"sample2\",\"age\":20}";  
  24.   
  25.         // JSON形式のデータをJavaのclassに変換(デコード)  
  26.         final Hoge hoge2 = JSON.decode(hogeJson2, Hoge.class);  
  27.         System.out.println("hoge2.name:" + hoge2.getName());  
  28.         System.out.println("hoge2.age:" + hoge2.getAge());  
  29.   
  30.         // 配列を含むJSON形式のデータを用意  
  31.         final String hoges = "{\"id\":\"10\",\"hoges\":[{\"name\":\"test\",\"age\":1},{\"name\":\"sample2\",\"age\":2}]}";  
  32.   
  33.         final HogeList hogeList = JSON.decode(hoges, HogeList.class);  
  34.   
  35.         // hogesのようにHogeの形式の配列を持つJSONを、HogeクラスのListをもつクラスにデコードすることが出来ます  
  36.         System.out.println("hogeList.id:" + hogeList.getId());  
  37.         for (final Hoge tmp : hogeList.getHoges()) {  
  38.             System.out.println("tmp.name:" + tmp.getName());  
  39.             System.out.println("tmp.age:" + tmp.getAge());  
  40.         }  
  41.     }  
  42.   
  43.     /** 
  44.      * JSONに対応するクラス 
  45.      * 各フィールド名をJSONのKEY名と同じにする 
  46.      * JSONのKEY名が 「sample-name」のような場合は「sampleName」とする 
  47.      */  
  48.     class Hoge {  
  49.   
  50.         private String name;  
  51.   
  52.         private int age;  
  53.   
  54.         public void setName(final String name) {  
  55.             this.name = name;  
  56.         }  
  57.   
  58.         public String getName() {  
  59.             return name;  
  60.         }  
  61.   
  62.         public void setAge(final int age) {  
  63.             this.age = age;  
  64.         }  
  65.   
  66.         public int getAge() {  
  67.             return age;  
  68.         }  
  69.     }  
  70.   
  71.     /** 
  72.      * HogeクラスのListを保持するクラス 
  73.      */  
  74.     class HogeList {  
  75.   
  76.         private String id;  
  77.   
  78.         private List<Hoge> hoges;  
  79.   
  80.         public void setId(final String id) {  
  81.             this.id = id;  
  82.         }  
  83.   
  84.         public String getId() {  
  85.             return id;  
  86.         }  
  87.   
  88.         public void setHoges(final List<Hoge> hoges) {  
  89.             this.hoges = hoges;  
  90.         }  
  91.   
  92.         public List<Hoge> getHoges() {  
  93.             return hoges;  
  94.         }  
  95.     }  
  96. }  

こんな感じです。フィールドの型の自動変換についてはかなり柔軟に対応してくれるみたいです。
詳しくは、JSONICのページを見てください;

1 件のコメント:

  1. If you need to hire a real hacker to remotely monitor / hack your partner's phone, recover your stolen bitcoin / any other cryptocurrency, or hack a database with guaranteed privacy, contact easybinarysolutions@gmail.com or whatsapp: +1 3478577580, they are efficient and confidential.

    返信削除