Created
October 10, 2010 06:46
-
-
Save kanemu/619030 to your computer and use it in GitHub Desktop.
[groovy]100本斬りお題39
This file contains 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
// g100pon #39 JSONの読み書き。Grails無しで | |
/* | |
/works/test/ichiro.json | |
------------------------ | |
{ | |
"名前" : { "first" : "一朗", "last" : "鈴木" }, | |
"打率" : ".315", | |
"打点" : "43", | |
"本塁打" : "6" | |
} | |
------------------------ | |
*/ | |
import org.codehaus.jackson.map.ObjectMapper | |
@Grab(group='org.codehaus.jackson', module='jackson-mapper-lgpl', version='1.6.0') | |
//読み込み | |
String jsonText = new File("/works/test/ichiro.json").getText("UTF8") | |
def map = new ObjectMapper().readValue(jsonText, Map) | |
assert map == ["名前":["first":"一朗", "last":"鈴木"], "打率":".315", "打点":"43", "本塁打":"6"] | |
assert map.getClass().toString() == "class java.util.LinkedHashMap" | |
//書き出し | |
map."安打" = "214" | |
new ObjectMapper().writeValue(new File('/works/test/ichiro_2.json'),map); | |
/* | |
/works/test/ichiro_2.json | |
------------------------ | |
{"名前":{"first":"一朗","last":"鈴木"},"打率":".315","打点":"43","本塁打":"6","安打":"214"} | |
------------------------ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment