json 对象post 请求怎样写类型转换器才能注入到struts 的List
与梦想赛跑 2014-11-19 10:36:06
action 有如下属性
public class GoodsAttributeAction  extends BaseAction {
private List<GoodsAttribute> attributeList = new ArrayList<GoodsAttribute>();
private int categoryId;// 分类ID
public void setAttributeList(List<GoodsAttribute> attributeList) {
this.attributeList = attributeList;
}
public List<GoodsAttribute> getAttributeList() {
return attributeList;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public void addGoodsAttributes()
{
goodsAttributeService.insertGoodsAttribute(categoryId, attributeList);
}
}



public class GoodsAttribute {
private int goodsAttributeId;
private int goodsCategoryId;// 分类ID
private String goodsAttributeName;// 属性名如净重
private String goodsAttributeClass;// 数据类型
private String goodsAttributeUnit;// 单位
get...
set...
}


json 对象

var attrJson = {"categoryId":1,attributeList [{"goodsAttributeName":"name1","goodsCategoryId":1},{"goodsAttributeName":"name2","goodsCategoryId":2}]};

发现问题如下:
前台通过ajax post 请求到addGoodsAttributes方法
发现action 中的 categoryId 有值。
attributeList 数组大小为2正确,
但是数据中的GoodsAttribute 对象没有把JSON中的属性值填充上去。
是不是要写类型转换器啊,我想这样注入到LIst的对象中,比较方便。


var attrJson = {"categoryId":1,"attributeList[0].goodsAttributeName":"name0","attributeList[1].goodsAttributeName":"name1"}
这样请求到action 可注入到attributeList 对象中,对象也有值,但是这样不参保证json中的数据结构,所以想说上面的问题怎么解决。有没有高手知道啊。
...全文
215 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2014-11-21
  • 打赏
  • 举报
回复
你i转化为字符串啊。 后天json 再转对象。
missMeyo 2014-11-21
  • 打赏
  • 举报
回复
可以到后台把json解析为对象,json的提交一般不用属性映射。
zy_think123 2014-11-21
  • 打赏
  • 举报
回复
用第三方的解析工具就可以了,比如fast-json,或者Gson或者json-lib都可以的
  • 打赏
  • 举报
回复
List<Object> list1 = (List<Object>) JSONArray.toCollection(json); for (int i = 0;i<list1.size();i++) { JSONObject jsonObj = JSONObject.fromObject(list1.get(i)); Map<String,String> mapobj = (Map<String, String>) JSONObject.toBean(jsonObj,HashMap.class); Long startTime = (long) 0; Long endTime = (long) 0; Long sysdate = Long.parseLong(sdf.format(new Date())); for (String jsonkey : mapobj.keySet()) { if(jsonkey.equals("topStartTime")){ if(null != mapobj.get(jsonkey) && !"".equals(mapobj.get(jsonkey))){ startTime = Long.parseLong(mapobj.get(jsonkey)); } } if(jsonkey.equals("topEndTime")){ if(null != mapobj.get(jsonkey) && !"".equals(mapobj.get(jsonkey))){ endTime = Long.parseLong(mapobj.get(jsonkey)); } } } if(sysdate >= startTime && sysdate <= endTime){ list.add(list1.get(i)); } else { list2.add(list1.get(i)); } } list.addAll(list2);
Super_Ninja 2014-11-20
  • 打赏
  • 举报
回复
看下这个,将json转化为对象 http://blog.csdn.net/caesardadi/article/details/11985183
程序袁_哈哈 2014-11-20
  • 打赏
  • 举报
回复
先在addGoodsAttributes方法中得到attrJson ,这里可以用string得到这个json格式的数据,然后用得到的string转化成jsonObject 得到里面的参数,再加入到你的list中去就好了。。。
shixitong 2014-11-19
  • 打赏
  • 举报
回复
//可以在这个方法里处理下,首先看看这个方法实际上传进来的是什么
public void setAttributeList(List<GoodsAttribute> attributeList) {
		 List<GoodsAttribute> retList=new ArrayList<GoodsAttribute>();
		 for(int i=0;i<attributeList.size();i++){
			Object obj = attributeList.get(i);
			//看看这个obj是什么,然后看看能不能把这个obj赋值给对应的GoodsAttribute
			//最后
			GoodsAttribute ga= new GoodsAttribute();
			ga.set……
			retList.add(ga);
		 }
         this.attributeList  = retList;
     }
2、直接传一个json字符串到后台,然后用第三方的JSON解析工具,把json字符串解析为具体的对象

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧