Created
June 8, 2013 18:17
-
-
Save lotusnet/5736097 to your computer and use it in GitHub Desktop.
Jackson を使用したJSONデータとPOJOの相互変換の実装サンプルです。
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 org.codehaus.jackson.JsonGenerationException; | |
| import org.codehaus.jackson.JsonParseException; | |
| import org.codehaus.jackson.map.JsonMappingException; | |
| import org.codehaus.jackson.map.ObjectMapper; | |
| public class User { | |
| // MEMO : Jackson用のフィールド定義 | |
| private int id; | |
| private String name; | |
| private String password; | |
| private Date entryDate; | |
| /** | |
| * IDを取得します。 | |
| * @return ID | |
| */ | |
| public int getId() { | |
| return id; | |
| } | |
| /** | |
| * IDを設定します。 | |
| * @param id ID | |
| */ | |
| public void setId(int id) { | |
| this.id = id; | |
| } | |
| /** | |
| * ユーザー名を取得します。 | |
| * @return ユーザー名 | |
| */ | |
| public String getName() { | |
| return name; | |
| } | |
| /** | |
| * ユーザー名を設定します。 | |
| * @param name ユーザー名 | |
| */ | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| /** | |
| * パスワードを取得します。 | |
| * @return パスワード | |
| */ | |
| public String getPassword() { | |
| return password; | |
| } | |
| /** | |
| * パスワードを設定します。 | |
| * @param password パスワード | |
| */ | |
| public void setPassword(String password) { | |
| this.password = password; | |
| } | |
| /** | |
| * 登録日を取得します。 | |
| * @return 登録日 | |
| */ | |
| public Date getEntryDate() { | |
| return entryDate; | |
| } | |
| /** | |
| * 登録日を設定します。 | |
| * @param entryDate 登録日 | |
| */ | |
| public void setEntryDate(Date entryDate) { | |
| this.entryDate = entryDate; | |
| } | |
| /** | |
| * JSONデータを解析し、ユーザー情報に変換します。 | |
| * @param jsonText JSONテキスト | |
| * @return ユーザー情報 | |
| */ | |
| public static User parseUser(String jsonText) { | |
| User user = null; | |
| Log.d("Parse JSON", "parse user entity"); | |
| ObjectMapper mapper = new ObjectMapper(); | |
| SimpleDateFormat dateFormat = new | |
| SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy"); | |
| mapper.setDateFormat(dateFormat); | |
| try { | |
| user = mapper.readValue(jsonText, User.class); | |
| Log.d("ParserResult", user.getId() + ", " + user.getName() + ", " | |
| + user.getPassword() + "," + user.getEntryDate().toString()); | |
| } catch (JsonParseException e) { | |
| e.printStackTrace(); | |
| } catch (JsonMappingException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| return user; | |
| } | |
| /** | |
| * ユーザー情報からJSON文字列を生成します。 | |
| * @param user ユーザー情報 | |
| * @return JSON文字列 | |
| */ | |
| public static String createJsonText(User user) { | |
| String jsonText = null; | |
| ObjectMapper objectMapper = new ObjectMapper(); | |
| try { | |
| jsonText = objectMapper.writeValueAsString(user); | |
| } catch (JsonGenerationException e) { | |
| e.printStackTrace(); | |
| } catch (JsonMappingException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| return jsonText; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment