how to create json structure in java?
var abc = {"action":"Remove",
"d开发者_JS百科atatable":[
{"userid":"userid0","username":"name0"},
{"userid":"userid1","username":"name1"},
{"userid":"userid2","username":"name2"},
{"userid":"userid3","username":"name3"}
]
, "msgType":"success"};
How could I create above JSON structure in java and send it as response & at client side how could I parse it?
Use Google's gson library. It converts java objects to and from json.
I've used it and it's really good.
consider groovy: http://groovy.codehaus.org/gapi/groovy/json/JsonBuilder.html (won't help you on the client side though)
Simple as using springframework and mark your @Controller method with @ResponseBody to json-encode your pojos.
Also use @RequestBody to decode json back and if needed use jackson annotations to customize d/encoding
精彩评论