Java解析Json串

data是JSON类型的字符串,形式如:"{unitId:21284,templateId:1,unitType:1,TPdaNodeCheckedLogs:[{checkedTime:'2011-03-29 17:13:02',isChecked:1,latitude:0.0,longitude:0.0,personId:1205,personName:'aa',templateItemId:33},{checkedTime:'2011-03-29 17:29:08',isChecked:1,latitude:0.0,longitude:0.0,personId:1205,personName:'bb',templateItemId:40}]}";

 

 

public String saveTemp(String data) {

JSONObject jsonObject = JSONObject.fromObject(data);
    /****对时间做处理******/
//   String[] dateFormats = new String[] {"yyyy-MM-dd HH:mm:ss"};  
   String[] dateFormats = new String[] {"yyyy-MM-dd"};  
       JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
   
   Map map = new HashMap();
   map.put("TPdaNodeCheckedLogs", TPdaNodeCheckedLog.class);


  TPdaNodeRelateTemplate clientRe = JSONObject
    .toBean(jsonObject,TPdaNodeRelateTemplate.class,map);
   
   Set clientLog=clientRe.getTPdaNodeCheckedLogs();
   

   Long unitId=clientRe.getUnitId();
   Long templateId=clientRe.getTemplateId();


   List relate=this.getRelate(unitId,templateId);
   boolean boo=false;
   TPdaNodeRelateTemplate' serverRe = new TPdaNodeRelateTemplate'();
   //如果关联记录存在则更新
   if(relate!=null&&relate.size()>0){
     serverRe = (TPdaNodeRelateTemplate')relate.get(0);
   }
   serverRe.setTemplateId(clientRe.getTemplateId());
   serverRe.setUnitId(clientRe.getUnitId());
   serverRe.setUnitType(clientRe.getUnitType());

   serverRe.setUploadFlag(new Long(1));
   //如果关联记录存在则更新
   if(relate!=null&&relate.size()>0){
    this.update(serverRe);
    message = "0|更新成功!";
    boo=true;
   }
   else{
    
    this.save(serverRe);
    message = "0|上传成功!";
    boo=true;
   }

}

 

 

TPdaNodeRelateTemplate 类

 

public class TPdaNodeRelateTemplate implements java.io.Serializable {

 private Long id;
 private Long unitId;
 private Long templateId;
 private Long uploadFlag;
 private Long unitType;
 private Set TPdaNodeCheckedLogs = new HashSet(0);

 // Constructors

 /** default constructor */
 public TPdaNodeRelateTemplate() {
 }

 /** full constructor */
 public TPdaNodeRelateTemplate(Long unitId, Long templateId,
   Long uploadFlag, Long unitType, Set TPdaNodeCheckedLogs) {
  this.unitId = unitId;
  this.templateId = templateId;
  this.uploadFlag = uploadFlag;
  this.unitType = unitType;
  this.TPdaNodeCheckedLogs = TPdaNodeCheckedLogs;
 }

 // Property accessors

 public Long getId() {
  return this.id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public Long getUnitId() {
  return this.unitId;
 }

 public void setUnitId(Long unitId) {
  this.unitId = unitId;
 }

 public Long getTemplateId() {
  return this.templateId;
 }

 public void setTemplateId(Long templateId) {
  this.templateId = templateId;
 }

 public Long getUploadFlag() {
  return this.uploadFlag;
 }

 public void setUploadFlag(Long uploadFlag) {
  this.uploadFlag = uploadFlag;
 }

 

 public Long getUnitType() {
  return unitType;
 }

 public void setUnitType(Long unitType) {
  this.unitType = unitType;
 }

 public Set getTPdaNodeCheckedLogs() {
  return this.TPdaNodeCheckedLogs;
 }

 public void setTPdaNodeCheckedLogs(Set TPdaNodeCheckedLogs) {
  this.TPdaNodeCheckedLogs = TPdaNodeCheckedLogs;
 }

 
 
}

 

 

TPdaNodeCheckedLog类

 

public class TPdaNodeCheckedLog extends cn.com.sgcc.core.base.BaseBean
  implements java.io.Serializable {

 // Fields

 private Long id;
 private Long relateId;
 private String longitude;
 private String latitude;
 private Long templateItemId;
 private Long isChecked;
 private Long personId;
 private String personName;
 private String checkedTime;
 private Long uploadFlag;

 // Constructors

 /** default constructor */
 public TPdaNodeCheckedLog() {
 }

 /** full constructor */
 public TPdaNodeCheckedLog(Long relateId,String longitude, String latitude, Long templateItemId,
   Long isChecked, Long personId, String personName, String checkedTime,
   Long uploadFlag) {
  this.longitude = longitude;
  this.latitude = latitude;
  this.templateItemId = templateItemId;
  this.isChecked = isChecked;
  this.personId = personId;
  this.personName = personName;
  this.checkedTime = checkedTime;
  this.uploadFlag = uploadFlag;
 }

 // Property accessors

 public Long getId() {
  return this.id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 
 
 public Long getRelateId() {
  return relateId;
 }

 public void setRelateId(Long relateId) {
  this.relateId = relateId;
 }

 public String getLongitude() {
  return this.longitude;
 }

 public void setLongitude(String longitude) {
  this.longitude = longitude;
 }

 public String getLatitude() {
  return this.latitude;
 }

 public void setLatitude(String latitude) {
  this.latitude = latitude;
 }

 public Long getTemplateItemId() {
  return this.templateItemId;
 }

 public void setTemplateItemId(Long templateItemId) {
  this.templateItemId = templateItemId;
 }

 public Long getIsChecked() {
  return this.isChecked;
 }

 public void setIsChecked(Long isChecked) {
  this.isChecked = isChecked;
 }

 public Long getPersonId() {
  return this.personId;
 }

 public void setPersonId(Long personId) {
  this.personId = personId;
 }

 public String getPersonName() {
  return this.personName;
 }

 public void setPersonName(String personName) {
  this.personName = personName;
 }

 public String getCheckedTime() {
  return this.checkedTime;
 }

 public void setCheckedTime(String checkedTime) {
  this.checkedTime = checkedTime;
 }

 public Long getUploadFlag() {
  return this.uploadFlag;
 }

 public void setUploadFlag(Long uploadFlag) {
  this.uploadFlag = uploadFlag;
 }

}

 

 

 

http://www.javaeye.com/topic/168818这个帖子很值得一看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值