Json和实体对象的转换

1.java实体对象转化为json数据:

后台代码:

   String id = request.getParameter("id");
   List serviceList = channelService.serviceList(Integer.parseInt(id));//查询出一条实体对象

   PrintWriter out = response.getWriter();
   JSONArray jsonArr = JSONArray.fromObject(serviceList);     
   JSONObject json = new JSONObject();           
   System.out.println(jsonArr);  
   out.print(jsonArr);  
   json.put("jsonArr", jsonArr);

前台获取:

function changePDID(pdid){
  var htmlStr = "";
  var cnum=Math.ceil(Math.random()*100);
            var param = {
    pdid : pdid,
    cnum : cnum
       }
            var http_value = jQuery.param(param);
      var url = "${pageContext.request.contextPath}/spServerAction/getServiceByPdID.do?pdid="+pdid;
 
   $.getJSON(url, http_value, function(data){
                document.getElementById("serviceid).options.length = 0;
                var htmlStr="";
                for (i = 0; i < data.length; i++) {
                    htmlStr += "";
                }
                $("#serviceid").append(htmlStr);
            });
}

 

 

2.json对象转化为java实体对象

前台代码:

function toJson(){
 var serviceid = document.getElementsByName("serviceid");
 var pdid = document.getElementsByName("pdid");
 var starttime = document.getElementsByName("starttime");
 var endtime = document.getElementsByName("endtime");
 legth=serviceid.length;
 var jsons="";
 for(var i=0;i  var serviceidvalue = serviceid[i].value;
  var pdidvalue = pdid[i].value;
  var starttimevalue = starttime[i].value;
  var endtimevalue = endtime[i].value;
  if(i==0)
   jsons+="[{serviceid:"+serviceidvalue+",pdid:"+pdidvalue+",starttime:"+starttimevalue+",endtime:"+endtimevalue+"}";
  else if(i==legth-1)
   jsons+=",{serviceid:"+serviceidvalue+",pdid:"+pdidvalue+",starttime:"+starttimevalue+",endtime:"+endtimevalue+"}]";
  else
   jsons+=",{serviceid:"+serviceidvalue+",pdid:"+pdidvalue+",starttime:"+starttimevalue+",endtime:"+endtimevalue+"}";
 }
 alert(jsons); //输出内容:[{"serviceid":5555,"pdid":135,"starttime":99,"endtime":99},{"serviceid":111,"pdid":137,"starttime":111,"endtime":111}]
 document.forms.frm_t.action="${pageContext.request.contextPath}/spServerAction/updateSpServer.do?jsons="+jsons;
 document.forms.frm_t.submit();
 
}

后台代码:

//解析json数据
   String jsons = request.getParameter("jsons");
   //JSONArray jtimesplit  = new JSONArray();
   JSONArray jtimesplit = new JSONArray().fromObject(new String(jsons));
   System.out.println(jtimesplit);
   List addSplitList = new ArrayList();
   for(int i=0;i    SpServerServiceSplit sptimeSplit = (SpServerServiceSplit)net.sf.json.JSONObject.toBean((net.sf.json.JSONObject) jtimesplit.get(i), SpServerServiceSplit.class);
    //sptimeSplit.setId(Integer.parseInt(id));
    sptimeSplit.setSpServer(spServer);//设一对多关系了,set一的表
    addSplitList.add(sptimeSplit);//循环加入到List集合中
    System.out.println(sptimeSplit.getPdid());
   }

spServer.setSpServerServiceSplit(addSplitList);//主表给子表赋值(List)
spServerService.insertSpServer(spServer);//只插入主表子表对应插入

 

 

 

json数据转化为实体简单例子如下:

public class TestJson {

 public static void main(String args []) {
  /*
  Person p1 = new Person("张一", "23", "男","浦东张江");
  Person p2 = new Person("张二", "21", "女","宝山友谊路");
  Person p3 = new Person("张三", "26", "男","闸北");
  Person p4 = new Person("张四", "22", "女","徐汇");
  */
  
  boolean[] barray = new boolean[]{true, false, true, false};
  JSONArray jarray = JSONArray.fromObject(barray);
  System.out.println(jarray);
  
  String jstr = "{name:\"李一\",age:\"22\",gender:\"男\",address:\"上海\"}";
  JSONObject jobject = JSONObject.fromObject(new String(jstr));
  Person p5 = (Person) JSONObject.toBean(jobject, Person.class);

  //JSONObject jsonobject = JSONObject.fromObject(new Person("张三", "23", "男","浦东张江"));
  //System.out.println(jsonobject);
  System.out.println(p5);
  
 }
}

你可能感兴趣的:(Java)