json组装

来源:互联网 发布:java断点续传原理 编辑:程序博客网 时间:2024/04/28 06:29
组装json:
1,建立对应结构的实体:
一层括号一个类(多个实体分开来建立,否则识别不到)
json到实体没有重复的属性数不需要executive的,list的格式会转化类变成MorphDynaBean。
======
你需要在JSONObject.toBean的时候把list的元素类型当成参数传进去。代码如下,你肯定看得懂MapClass> classMap = new HashMapClass>();classMap.put("list", B.class);A j = (A) JSONObject.toBean(json, A.class,classMap);

======
实体到json有建立的jpa相互关联的要排除,没建立jpa对应的不许排除,直接转化
排除1,实体中注解排除@Entity
@JsonIgnoreProperties(value={"processLogs"})属性不进行json的转化,忽略这个属性的json转化
2,json时executive
中途有[]重复多个的为list
public String getWorkProcessLogById() {
String id=model.getId();
List<ProcessLog> a = new ArrayList<ProcessLog>();
a= this.getService().getWorkProcessLogById(id);
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"troubleProcesOrder","workOrder","notice"});

String json = JSONArray.fromObject(a, config).toString();
  HttpServletResponse response =  ServletActionContext.getResponse();;
response.setCharacterEncoding("utf-8");
        ServletOutputStream out = null;
try
        {
        out = response.getOutputStream();
            out.write(json.getBytes());
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
        try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        }
        return null;
}


package Test;


import com.ces.zwww.entity.GisObject;
import com.ces.zwww.utils.JsonUtil;

public class Test {


public static void main(String[] args) {
String res="[{" +
"'id' : '3b04b3aa-939e-4cab-bb16-13e515a52f57'," +
"'createTime' :'2014-07-28 14:3'," +
"'status' : '一线处理中'," +
"'currentActors' :'顶级管理员'," +
"'creator' : '系统'," +
"'closer' : null," +
"'appealUserEmail' : null," +
"'tecCategory' : null," +
"'kind' : '故障','faultCause' : null," +
"'related_knowledge' : null," +
"'appealUserOrg' : null," +
"'closeTime' : null," +
"'oneTimeSuccess' : '是'," +
"'scoreTime' : null," +
"'relatedTicket' : null," +
   "'importance' : '中'," +
   "'outId' : null," +
   "'appealApproveOpinion' : null," +
   "'relateAsset' : null," +
   "'relateResource' : null," +
   "'attachFile_Solve' : null," +
   "'occurTime' : '2014-07-28 14:35'," +
   "'outOfSla' : '否'," +
   "'title' : '测试demo'," +
   "'attachFile' : null," +
   "'priority' : '中(1天)'," +
   "'appealUserPhone' : null," +
   "'solver' : null," +
   "'recordTime' : '2014-07-28 14:35'," +
   "'urgency' : '中'," +
   "'flowNo' : 'SJ_20140728_0008'," +
   "'appealUserId' : null," +
   "'detail' : '事件描述'," +
   "'slaNoticeSent' : '否'," +
   "'appealUserName' : null," +
   "'briefSolvent' : null," +
   "'incidentSource' : '监控系统'," +
   "'closeCode' : null," +
   "'solvent' : null," +
   "'announcer' : '系统'," +
   "'faultType1' : null," +
   "'faultType2' : null," +
   "'announcerDept' : null," +
   "'category' : '文档服务器'," +
   "'score' : null," +
   "'solveTime' : null," +
   "'solveMode' : null," +
   "'planSolveTime' : null," +
   "'announceTime' : '2014-07-28 14:35' ," +
   "'processLogs' : [ {" +
   "'stage' : '新建事件'," +
   "'actorId' : 'admin'," +
   "'actorName' : '顶级管理员'," +
   "'submit' : '交办'," +
   "'memo' : '同意'," +
   "'recordTime' : '2014-07-18 11:34'," +
   "'cnName' : null" +
   "}, {" +
   "'stage' : '一线处理中'," +
   "'actorId' : 'admin'," +
   "'actorName' : '顶级管理员'," +
   "'submit' : '子流程'," +
   "'memo' : '请修改'," +
   "'recordTime' : '2014-07-18 11:34'," +
   "'cnName' : null" +
       "}]" +
"}]";
String aa ="{" +
"'$310106':{" +
"'bgcolor':'0xFF0000', " +
"'opacity':'10'," +
"'tooltip':" +
"{" +
"'url':'http://www.baidu.com', " +
"'div':" +
" [{ " +
"'ico':'ico',  " +
"'txt': '30/800'" +
"}," +
"{" +
"'ico': 'ico1',  " +
"'txt': '30/600'" +
"}]," +
"'type':'0'" +
"}" +
"}," +
"'$310107':{" +
"'bgcolor':'0x00FF00', " +
"'opacity':'5'," +
"'tooltip':" +
"{" +
"'url':'http://www.sohu.com', " +
"'div':" +
" [{ " +
"'ico': 'ico2',  " +
"'txt': '30/800'" +
"}," +
"{" +
"'ico': 'ico3',  " +
"'txt': '30/600'" +
"}]," +
"'type':'0'" +
"}" +
"}" +
"}";
//Srtring a =JsonUtil.
System.out.println(res);
GisObject a= (GisObject) JsonUtil.jsonStrToObjectOb(aa, GisObject.class);
String b =JsonUtil.objectToJsonStr(a);
System.out.println(b);

}
}




/**
*
*/
package com.ces.zwww.entity;


/**
* @author yu hui
*
*
* 割接工单表
*
*/

public class GisObject  {

private Region $310106;
private Region $310107;
/**
* @return the $310106
*/
public Region get$310106() {
return $310106;
}
/**
* @param $310106 the $310106 to set
*/
public void set$310106(Region $310106) {
this.$310106 = $310106;
}
/**
* @return the $310107
*/
public Region get$310107() {
return $310107;
}
/**
* @param $310107 the $310107 to set
*/
public void set$310107(Region $310107) {
this.$310107 = $310107;
}



}





package com.ces.zwww.entity;

import java.util.List;



public class Region {
public String bgcolor;
public String opacity;
public Tip tooltip;
/**
* @return the bgcolor
*/
public String getBgcolor() {
return bgcolor;
}
/**
* @param bgcolor the bgcolor to set
*/
public void setBgcolor(String bgcolor) {
this.bgcolor = bgcolor;
}
/**
* @return the opacity
*/
public String getOpacity() {
return opacity;
}
/**
* @param opacity the opacity to set
*/
public void setOpacity(String opacity) {
this.opacity = opacity;
}
/**
* @return the tooltip
*/
public Tip getTooltip() {
return tooltip;
}
/**
* @param tooltip the tooltip to set
*/
public void setTooltip(Tip tooltip) {
this.tooltip = tooltip;
}

}


package com.ces.zwww.entity;

import java.util.List;


public class Tip {
public String url;
public List<Div> div;
public String type;
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return the div
*/
public List<Div> getDiv() {
return div;
}
/**
* @param div the div to set
*/
public void setDiv(List<Div> div) {
this.div = div;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}


}



package com.ces.zwww.entity;

public class Div {
public String ico;
public String txt;
/**
* @return the ico
*/
public String getIco() {
return ico;
}
/**
* @param ico the ico to set
*/
public void setIco(String ico) {
this.ico = ico;
}
/**
* @return the txt
*/
public String getTxt() {
return txt;
}
/**
* @param txt the txt to set
*/
public void setTxt(String txt) {
this.txt = txt;
}

}
0 0
原创粉丝点击