开发者

how to create json structure in java?

开发者 https://www.devze.com 2023-04-06 04:50 出处:网络
var abc = {\"action\":\"Remove\", \"d开发者_JS百科atatable\":[ {\"userid\":\"userid0\",\"username\":\"name0\"},
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

0

精彩评论

暂无评论...
验证码 换一张
取 消