问题描述
如题,客户端要传如下格式的json数据到服务器端,其中名为page和parameter的key名是确定的,但它们各自数组中的key名和个数是不确定的:"{'page':[{'id':'100001','create_by':'system','userinfo':'userinfo','role':'role'},"+"{'id':'100002','create_time':'2013-07-1311:00:00','create_by':'sync....'}],"+"'parameter':[{'arg001':'2046-08-08','arg002':'2019-08-08'}]}"
请问在websevice层或controller层定义的java接口方法中,要怎样定义参数来接收这样的json数据?
解决方案
本帖最后由 lblhmz 于 2014-08-23 14:34:23 编辑
解决方案二:
请参看如下代码{"response":{"product_list":[{"state":null,"month_product_id":1000003139816,"month_product_name":"成功必读包月区","order_date":null,"month_product_description":"是通货膨胀还是金融风暴?是投资理财还是风险控制?8元经管好书看个够,做都市精英。rn","account_msg":800,"readpoint_price":"800","cover_path":"http://61.130.247.183/","wap_url":"http://wapread.189.cn/baoyueInfoListAction.action?monthProductId=1000003139816"},{"state":null,"month_product_id":1000003139847,"month_product_name":"巅峰武侠包月区","order_date":null,"month_product_description":"自任侠客,仗剑江湖为红颜,侠之大为国为民。5元50本武林宝典,每月更新10本。rn","account_msg":500,"readpoint_price":"500","cover_path":"http://61.130.247.183/","wap_url":"http://wapread.189.cn/baoyueInfoListAction.action?monthProductId=1000003139847"},{"state":null,"month_product_id":1000003706336,"month_product_name":"霓彩都市包月区","order_date":null,"month_product_description":"白手起家成巨富,异能法宝战都市,草根崛起!。5元50本都市爽文,每月更新10本。rn","account_msg":500,"readpoint_price":"500","cover_path":"http://61.130.247.183/","wap_url":"http://wapread.189.cn/baoyueInfoListAction.action?monthProductId=1000003706336"}]}}
result为以上json字符串。JSONObjectobject=JSONObject.fromObject(result);JSONObjectresponseObj=object.getJSONObject("response");JSONArrayjsonArray=responseObj.getJSONArray("product_list");//通过遍历JSONArray对象获得JSONObject对象,并将其转为字符串for(inti=0;i<jsonArray.size();i++){JSONObjectjsonObj=((JSONObject)jsonArray.opt(i));//使用Gson类的fromJson对象将json数据和实体类进行映射MonthProductBeanmpBean=gson.fromJson(jsonObj.toString(),MonthProductBean.class);list.add(mpBean);}
解决方案三:
关键是后台接口的参数怎么定义?或者说这个json字符串怎么传到后台引用1楼askwei007的回复:
请参看如下代码{"response":{"product_list":[{"state":null,"month_product_id":1000003139816,"month_product_name":"成功必读包月区","order_date":null,"month_product_description":"是通货膨胀还是金融风暴?是投资理财还是风险控制?8元经管好书看个够,做都市精英。rn","account_msg":800,"readpoint_price":"800","cover_path":"http://61.130.247.183/","wap_url":"http://wapread.189.cn/baoyueInfoListAction.action?monthProductId=1000003139816"},{"state":null,"month_product_id":1000003139847,"month_product_name":"巅峰武侠包月区","order_date":null,"month_product_description":"自任侠客,仗剑江湖为红颜,侠之大为国为民。5元50本武林宝典,每月更新10本。rn","account_msg":500,"readpoint_price":"500","cover_path":"http://61.130.247.183/","wap_url":"http://wapread.189.cn/baoyueInfoListAction.action?monthProductId=1000003139847"},{"state":null,"month_product_id":1000003706336,"month_product_name":"霓彩都市包月区","order_date":null,"month_product_description":"白手起家成巨富,异能法宝战都市,草根崛起!。5元50本都市爽文,每月更新10本。rn","account_msg":500,"readpoint_price":"500","cover_path":"http://61.130.247.183/","wap_url":"http://wapread.189.cn/baoyueInfoListAction.action?monthProductId=1000003706336"}]}}result为以上json字符串。JSONObjectobject=JSONObject.fromObject(result);JSONObjectresponseObj=object.getJSONObject("response");JSONArrayjsonArray=responseObj.getJSONArray("product_list");//通过遍历JSONArray对象获得JSONObject对象,并将其转为字符串for(inti=0;i<jsonArray.size();i++){JSONObjectjsonObj=((JSONObject)jsonArray.opt(i));//使用Gson类的fromJson对象将json数据和实体类进行映射MonthProductBeanmpBean=gson.fromJson(jsonObj.toString(),MonthProductBean.class);list.add(mpBean);}
解决方案四:
取到之后遍历一下不就好了