Ext.grid数据用json格式传到后台方式

页面获取store并转换json格式

//得到JSON格式的数据

function getJSONData(store){
var detailURL="";
var count=store.getCount();
for(var i=0;i < count;i++){
var temp=store.getAt(i);
if(""!=detailURL){
detailURL=detailURL+",";
}
detailURL+=Ext.util.JSON.encode(temp.data);
}
detailURL=("["+detailURL+"]");
return detailURL;

}

sturts接收json字符串

private String jsonString;

用方法把store转换成List


/**
* 通过JSON获取数据

* @param <T>
* @param jsonString
* @param cls
* @return
*/
@SuppressWarnings("unchecked")
private <T> Collection<T> getDataFromJSON(String jsonString, Class<T> cls,
String... excludeFieldName) {
Collection<T> detailCollection = null;
if (null != jsonString) {
JSONArray fromObject = JSONArray.fromObject(jsonString);
int size = fromObject.size();
for (int i = 0; i < size; i++) {
JSONObject object = (JSONObject) fromObject.get(i);
for (int j = 0; j < excludeFieldName.length; j++) {
String string = excludeFieldName[j];
object.remove(string);
}
}
detailCollection = JSONArray.toCollection(fromObject, cls);
} else {
detailCollection = new ArrayList<T>();
}
return detailCollection;
}


Collection<T> ts = getDataFromJSON(jsonString, T.class);


发布了52 篇原创文章 · 获赞 12 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/caodegao/article/details/8748451