JSONエンコード/デコードサンプルプログラム
Last active
September 19, 2015 05:18
-
-
Save tasukujp/263ce90ec68067e6acfd to your computer and use it in GitHub Desktop.
JSONICの使い方
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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