实体类:
public class Student {
public Integer sid;
public String name;
public String teacher;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
}
测试:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import net.sf.json.JSONArray;
public class List2Json {
public static void main(String[] args) {
List<Object> list = new ArrayList<Object>();
//创建对象并实例化
Student st = new Student();
Student st1 = new Student();
st.setSid(1);
st.setName("alec");
st.setTeacher("scott");
st1.setSid(2);
st1.setName("xlj");
st1.setTeacher("苍井null");
//对象放到list集合里
list.add(st);
list.add(st1);
JSONArray json = new JSONArray();//JSON数组,调用addAll()方法把集合转化成json数组
json.addAll(list);
json.listIterator();
if(json.isEmpty()){
System.out.println("dfdfdf");
}
//对json集合进行遍历
ListIterator li = json.listIterator();
while(li.hasNext()){
System.out.println(li.next().toString());
}
// System.out.println(json);
}
}
JSONObject与JSONArray的demo
public JSONObject fundCharts(HttpServletRequest request,HttpServletResponse response,String code,int timecs){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String begintime,endtime;
Calendar cal = Calendar.getInstance();
if(timecs!=0){
cal.add(Calendar.MONTH, timecs);
}else{
cal.add(Calendar.MONTH, -3);
}
begintime = sdf.format(cal.getTime());
Calendar cal2 = Calendar.getInstance();
cal2.add(Calendar.DATE, -1);
endtime = sdf.format(cal2.getTime());
//产品总净值json
JSONObject jsonObject = new JSONObject();
//普通级净值json
JSONObject jsonObject2 = new JSONObject();
//优先级净值json
JSONObject jsonObject3 = new JSONObject();
//时间
JSONObject jsonObject4 = new JSONObject();
//存放jsonobject的数组
JSONArray jsonData = new JSONArray();
ArrayList<TbProductDay2> tblist=tBProductDayService.findCharts(code/*,begintime,endtime*/);
//普通级净值
ArrayList<Double> productvalue=new ArrayList<Double>();
//优先级净值
ArrayList<Double> productprivalue=new ArrayList<Double>();
//产品总净值
ArrayList<Double> producttotalnetval=new ArrayList<Double>();
//
ArrayList<String> productdate=new ArrayList<String>();
for(int i=0;i<tblist.size();i++){
TbProductDay2 tb=tblist.get(i);
//普通
Double pt=tb.getI_dayretrace();
//优先
Double yx=tb.getI_productprivalue();
//总净值
Double zjz=tb.getI_producttotalnetval();
//时间
Date d=tb.getC_productdate();
String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(d);
productvalue.add(pt);
productprivalue.add(yx);
producttotalnetval.add(zjz);
productdate.add(dateStr);
}
jsonObject.put("name", "产品总净值");
jsonObject2.put("name", "劣后级净值");
jsonObject3.put("name", "优先级净值");
jsonObject.put("data", productprivalue);
jsonObject2.put("data", productprivalue);
jsonObject3.put("data", producttotalnetval);
jsonObject4.put("categories", productdate);
jsonData.add(jsonObject);
jsonData.add(jsonObject2);
jsonData.add(jsonObject3);
//request.setAttribute("jsonObject4",jsonObject4);
//request.setAttribute("jsonData",jsonData.toString());
//return "/hchars/htmlcode";
return jsonObject4;
}
转载于:https://blog.51cto.com/xuliangjun/1429593