Skip to content

Instantly share code, notes, and snippets.

@kanemu
Created October 10, 2010 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanemu/619030 to your computer and use it in GitHub Desktop.
Save kanemu/619030 to your computer and use it in GitHub Desktop.
[groovy]100本斬りお題39
// 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