JSONICã§ãªãã¸ã§ã¯ããJSONå½¢å¼ã«å¤æãã
JSONICã使ãã¨ãä»»æã®JavaBeanãJSONã«ããã£ã¨å¤æã§ãã¾ãã
// ãã¹ãç¨ã¯ã©ã¹ static final class Kitten { private String name; private int age; public Kitten() {} public Kitten( String name, int age ) { this.name = name; this.age = age; } public final String getName () { return name; } public final void setName ( String name ) { this.name = name; } public final int getAge () { return age; } public final void setAge ( int age ) { this.age = age; } @Override public String toString() { return name + ":" + age; } } ... // ãã¹ãç¨ãªãã¸ã§ã¯ã Kitten mii = new Kitten("mii", 1); // jsonã«ã¨ã³ã³ã¼ã String json = JSON.encode( mii, true ); System.out.println( json ); // jsonãããªãã¸ã§ã¯ããçæ mii = JSON.decode( json, Kitten.class ); System.out.println( mii.toString() );
å®è¡çµæã§ãã
{ "age": 1, "name": "mii" } mii:1
ãã¹ããã¦ãããªãã¸ã§ã¯ãã®å¤æããµãã¼ãããã¦ãã¾ãã
// ãã¹ãç¨ã¯ã©ã¹ static final class Kitten { private String name; private int age; private Kitten friend; // 追å public Kitten() {} public Kitten( String name, int age ) { this.name = name; this.age = age; } public final String getName () { return name; } public final void setName ( String name ) { this.name = name; } public final int getAge () { return age; } public final void setAge ( int age ) { this.age = age; } public final Kitten getFriend () { return friend; } public final void setFriend ( Kitten friend ) { this.friend = friend; } @Override public String toString() { return name + ":" + age; } } ... Kitten mii = new Kitten("mii", 1); Kitten tora = new Kitten("tora", 2); mii.setFriend( tora ); // jsonã«ã¨ã³ã³ã¼ã String json = JSON.encode( mii, true ); System.out.println( json ); // jsonãããªãã¸ã§ã¯ããçæ mii = JSON.decode( json, Kitten.class ); System.out.println( mii.toString() ); System.out.println( mii.getFriend().toString() );
å®è¡çµæã§ãã
{ "age": 1, "friend": { "age": 2, "friend": null, "name": "tora" }, "name": "mii" } mii:1 tora:2