如何在jsp页面中利用小脚本将list转换为json
发布网友
发布时间:2022-04-26 18:12
我来回答
共4个回答
热心网友
时间:2022-04-23 17:24
脚本把list转换为json,还有就是,假如我在jsp页面中定义了一个方法来转换list为json,
------解决方案--------------------------------------------------------
使用json-lib 包来进行java 对象和json 之间的相互转换
JSONArray jsonArray=JSONArray.fromObject(list);//将list 转换为json 数组
response.setContentType( application/json );//设置返回的数据为json 对象
如果是jsp 的页面直接用out.print(jsonArray);注意统一编码
servlet 或者action 中都用response 对象getWriter.print(jsonArray)
那你的请求地址是jsp 页面还是servlet 还是action。请求哪个就写哪个url 就是了
为了简单都用jquery 来进行ajax
$.getJSON( 你的 url 地址 ,function(data){//回调函数接收返回的 json 格式数据data});
------解决方案--------------------------------------------------------用Jquery!
热心网友
时间:2022-04-23 18:42
可以使用Gson
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonTest1 {
public static void main(String[] args) {
Gson gson = new Gson();
Student student1 = new Student();
student1.setId(1);
student1.setName("李坤");
student1.setBirthDay(new Date());
// //////////////////////////////////////////////////////////
System.out.println("----------简单对象之间的转化-------------");
// 简单的bean转为json
String s1 = gson.toJson(student1);
System.out.println("简单Bean转化为Json===" + s1);
// json转为简单Bean
Student student = gson.fromJson(s1, Student.class);
System.out.println("Json转为简单Bean===" + student);
// 结果:
// 简单Bean转化为Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:27:52 AM"}
// Json转为简单Bean===Student [birthDay=Fri Jun 22 08:27:52 CST 2012, id=1,
// name=李坤]
// //////////////////////////////////////////////////////////
Student student2 = new Student();
student2.setId(2);
student2.setName("曹贵生");
student2.setBirthDay(new Date());
Student student3 = new Student();
student3.setId(3);
student3.setName("柳波");
student3.setBirthDay(new Date());
List<Student> list = new ArrayList<Student>();
list.add(student1);
list.add(student2);
list.add(student3);
System.out.println("----------带泛型的List之间的转化-------------");
// 带泛型的list转化为json
String s2 = gson.toJson(list);
System.out.println("带泛型的list转化为json==" + s2);
// json转为带泛型的list
List<Student> retList = gson.fromJson(s2,
new TypeToken<List<Student>>() {
}.getType());
for (Student stu : retList) {
System.out.println(stu);
}
// 结果:
// 带泛型的list转化为json==[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":2,"name":"曹贵生","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 8:28:52 AM"}]
// Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=1, name=李坤]
// Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=2, name=曹贵生]
// Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=3, name=柳波]
}
}
引用
http://blog.csdn.net/lk_blog/article/details/7685169
热心网友
时间:2022-04-23 20:17
=================定义List方法======================
function List(){
this.value = [];
/*添加*/
this.add = function(obj){
return this.value.push(obj);
};
/*大小*/
this.size = function(){
return this.value.length;
};
/*返回指定索引的值*/
this.get = function(index){
return this.value[index];
};
/*删除指定索引的值*/
this.remove = function(index){
this.value1 = [];
this.value[index]='';
for(var i=0;i<this.size();i++){
if(this.value[i]!=''){
this.value1.push(this.value[i]);
}
}
this.value = this.value1;
delete this.value1;
return this.value;
};
/*删除全部值*/
this.removeAll = function(){
return this.value=[];
};
/*是否包含某个对象*/
this.constains = function(obj){
for(var i in this.value){
if( obj == this.value[i] ){
return true;
}else{
continue;
}
}
return false;
};
}
====================new刚才新建的List用友存数据==============
var arr = new List();
var salesInfoDetail = new Object();
salesInfoDetail.sortId=trChld.eq(0).text();
salesInfoDetail.proctCode=trChld.eq(1).text();
salesInfoDetail.proctName=trChld.eq(2).text();
salesInfoDetail.salesAMT=trChld.eq(3).text();
salesInfoDetail.quantity=trChld.eq(4).text();
salesInfoDetail.proctCost=trChld.eq(5).text();
salesInfoDetail.perTotalCost=trChld.eq(6).text();
salesInfoDetail.bizCost=trChld.eq(7).text();
salesInfoDetail.presentCost=trChld.eq(8).text();
salesInfoDetail.countGap=trChld.eq(9).text();
salesInfoDetail.salesBenefit=trChld.eq(10).text();
salesInfoDetail.finalAvailableBenefit=trChld.eq(11).text();
salesInfoDetail.perComment=trChld.eq(12).text();
arr.add(salesInfoDetail);
var json = JSON.stringify(arr);
这是js 方法
在jsp中可以<%! %> 加入java代码 引用谷歌的 JSONObject 去直接转换
热心网友
时间:2022-04-23 22:08
var form = {
//属性名称:value值
startDate:startDate,
orderBy:orderType,
marketPlaceType:marketplaceType,
endDate:endDate,
top:top
};
inputJson = JSON.stringify(form);
alert(inputJson);