JSON-lib を使う

http://json-lib.sourceforge.net/

テストしてみた。

import net.sf.json.JSONObject;

public class App 
{
    public static void main( String[] args )
    {
	String src = "{\"name\":\"hoge\"}";
	JSONObject json = JSONObject.fromObject(src);
	System.out.println("name=" + json.get("name"));
    }
}

う〜む。依存ライブラリ多い・・・。maven2 で依存関係を解決して、ビルドとテストの環境を整えた。最近使ってないので、思い出しつつ。

 mvn archetype:create -DgroupId=json -DartifactId=json
 cd json

pom.xml に依存関係を追加してみたが…。

  <dependencies>
    <dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.2.1</version>
    </dependency>
    :

コンパイルを実行すると、失敗。json-lib をマニュアルでインストールせよとのこと。

 $ mvn compile

  :
  :

1) net.sf.json-lib:json-lib:jar:2.2.1

  Try downloading the file manually from the project website.

  Then, install it using the command: 
      mvn install:install-file -DgroupId=net.sf.json-lib -DartifactId=json-lib \
          -Dversion=2.2.1 -Dpackaging=jar -Dfile=/path/to/file

いまいち理由はよく分からんが、http://json-lib.sourceforge.net/ から取得した jar ファイルをインストール。

 mvn install:install-file -DgroupId=net.sf.json-lib -DartifactId=json-lib \
           -Dversion=2.2.1 -Dpackaging=jar -Dfile=../json-lib-2.2.1-jdk15.jar 

テストで実行してみる。(JUnit 用のテストプログラムも組んで。)

 mvn test