Skip to content

Instantly share code, notes, and snippets.

@tasukujp
Last active September 19, 2015 05:18
Show Gist options
  • Save tasukujp/263ce90ec68067e6acfd to your computer and use it in GitHub Desktop.
Save tasukujp/263ce90ec68067e6acfd to your computer and use it in GitHub Desktop.
JSONICの使い方
import net.arnx.jsonic.JSON;
import java.util.Arrays;
public class JsonicDecode {
public static void main(String[] args) {
String json = "{\"id\":1,\"name\":\"tasukujp\",\"hobby\":[\"running\",\"bouldering\"],\"interests\":[\"machine learning\",\"app\"],\"skill\":{\"Java\":true,\"Ruby\":null}}";
User user = JSON.decode(json, User.class);
System.out.println("id:" + user.getId());
System.out.println("name:" + user.getName());
user.getHobby().forEach(val -> System.out.println("Hobby:" + val));
Arrays.stream(user.getInterests()).forEach(val -> System.out.println("Interests:" + val));
System.out.println("skill:" + user.getSkill());
}
}
import net.arnx.jsonic.JSON;
import java.util.Arrays;
import java.util.HashMap;
public class JsonicEncode {
public static void main(String[] args) {
User user = new User();
user.setId(1);
user.setName("tasukujp");
user.setHobby(Arrays.asList("running", "bouldering", "baseball"));
user.setInterests(new String[] {"machine learning", "app"});
user.setSkill(new HashMap<String, Object>() {
{put("Java", true);}
{put("Ruby", null);}
});
String json = JSON.encode(user);
System.out.println(json);
}
}
import java.util.List;
import java.util.Map;
public class User {
private int id;
private String name;
private List<String> hobby;
private String[] interests;
private Map<String, Object> skill;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public List<String> getHobby() { return hobby; }
public void setHobby(List<String> hobby) { this.hobby = hobby; }
public String[] getInterests() { return interests; }
public void setInterests(String[] interests) { this.interests = interests; }
public Map<String, Object> getSkill() { return skill; }
public void setSkill(Map<String, Object> skill) { this.skill = skill; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment