POJO
POJO:AckDoc
package org.example.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
@JsonRootName(value="ackDoc")
public class AckDoc {
/**
* 返回头
*/
private AckDocHeader docHeader;
/**
* 返回内容
*/
private Object docRecord;
public AckDocHeader getDocHeader() {
return docHeader;
}
public void setDocHeader( AckDocHeader docHeader) {
this.docHeader = docHeader;
}
public Object getDocRecord() {
return docRecord;
}
public void setDocRecord(Object docRecord) {
this.docRecord = docRecord;
}
public AckDoc( AckDocHeader docHeader, Object docRecord) {
this.docHeader = docHeader;
this.docRecord = docRecord;
}
public AckDoc( AckDocHeader docHeader) {
this.docHeader = docHeader;
}
public AckDoc() {
}
@Override
public String toString() {
return "{" +
"docHeader=" + docHeader +
", docRecord=" + docRecord +
'}';
}
}
POJO:AckDocHeader
package org.example.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
public class AckDocHeader
{
/**
* 会话id
*/
private String sessionId;
/**
* 编码
*/
private String ackCode;
/**
* 内容
*/
private String ackMsg;
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public String getAckCode() {
return ackCode;
}
public void setAckCode(String ackCode) {
this.ackCode = ackCode;
}
public String getAckMsg() {
return ackMsg;
}
public void setAckMsg(String ackMsg) {
this.ackMsg = ackMsg;
}
public AckDocHeader() {
}
public AckDocHeader(String sessionId, String ackCode, String ackMsg) {
this.sessionId = sessionId;
this.ackCode = ackCode;
this.ackMsg = ackMsg;
}
public AckDocHeader(String ackCode, String ackMsg) {
this.ackCode = ackCode;
this.ackMsg = ackMsg;
}
@Override
public String toString() {
return "AckDocHeader{" +
"sessionId='" + sessionId + '\'' +
", ackCode='" + ackCode + '\'' +
", ackMsg='" + ackMsg + '\'' +
'}';
}
}
POJO:DemoTestListPojo
package org.example.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import java.util.List;
@JsonRootName(value="bigPojo")
public class DemoTestListPojo {
private List<DemoTestPojo> demoTestPojoList;
private String cmt ;
public List<DemoTestPojo> getDemoTestPojoList() {
return demoTestPojoList;
}
public void setDemoTestPojoList(List<DemoTestPojo> demoTestPojoList) {
this.demoTestPojoList = demoTestPojoList;
}
public String getCmt() {
return cmt;
}
public void setCmt(String cmt) {
this.cmt = cmt;
}
@Override
public String toString() {
return "DemoTestListPojo{" +
"demoTestPojoList=" + demoTestPojoList +
", cmt='" + cmt + '\'' +
'}';
}
}
POJO:DemoTestPojo
package org.example.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
public class DemoTestPojo {
private String name;
private String des;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
@Override
public String toString() {
return "DemoTestPojo{" +
"name='" + name + '\'' +
", des='" + des + '\'' +
'}';
}
}
FastJson
package org.example;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.pojo.AckDoc;
import org.example.pojo.AckDocHeader;
import org.example.pojo.DemoTestListPojo;
import org.example.pojo.DemoTestPojo;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
class AppFast
{
public static void main( String[] args ) throws Exception {
List<DemoTestPojo> lst = new ArrayList<DemoTestPojo>();
for(int i=0 ; i<3 ; i++)
{
DemoTestPojo dt = new DemoTestPojo();
dt.setName("name" + i);
dt.setDes("des");
lst.add(dt);
}
DemoTestListPojo dtlp = new DemoTestListPojo();
dtlp.setDemoTestPojoList(lst);
dtlp.setCmt("wwww");
AckDocHeader ah = new AckDocHeader("sessionid-1","0","msg-empty");
AckDoc ack = new AckDoc(ah,dtlp);
// AckDoc to json fastjson
String json = JSON.toJSONString(ack);
System.out.println("json = " +json);
// json to AckDoc fastjson
// 两种json转对象的方式效果一样。
// 第一种
//JSONObject jsonObject1 = JSONObject.parseObject(json);
//AckDoc ackDoc1 = jsonObject1.toJavaObject(AckDoc.class);
//System.out.println("ackDoc1 = " +ackDoc1);
// 第二种
AckDoc ackDoc = JSON.parseObject(json,AckDoc.class);
System.out.println("AckDoc = " +ackDoc);
JSONObject jsonObject = (JSONObject)ackDoc.getDocRecord();
DemoTestListPojo demoTestListPojo = jsonObject.toJavaObject(DemoTestListPojo.class);
List<DemoTestPojo> pojoList = demoTestListPojo.getDemoTestPojoList();
System.out.println("pojoList = " +pojoList);
String cmt = demoTestListPojo.getCmt();
System.out.println("cmt = " +cmt);
}
}
JackJson
package org.example;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.hsf.HSFJSONUtils;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import org.example.pojo.AckDoc;
import org.example.pojo.AckDocHeader;
import org.example.pojo.DemoTestListPojo;
import org.example.pojo.DemoTestPojo;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
/**
* Hello world!
*
*/
public class AppJack
{
public static void main( String[] args ) throws Exception {
List<DemoTestPojo> lst = new ArrayList<DemoTestPojo>();
for(int i=0 ; i<3 ; i++)
{
DemoTestPojo dt = new DemoTestPojo();
dt.setName("name" + i);
dt.setDes("des");
lst.add(dt);
}
DemoTestListPojo dtlp = new DemoTestListPojo();
dtlp.setDemoTestPojoList(lst);
dtlp.setCmt("wwww");
AckDocHeader ah = new AckDocHeader("sessionid-1","0","msg-empty");
AckDoc ack = new AckDoc(ah,dtlp);
// AckDoc to json jackjson
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(ack);
System.out.println("json = " +json);
// json to AckDoc jackjson
AckDoc ackDoc = (AckDoc) mapper.readValue(json, AckDoc.class);
System.out.println("AckDoc = " +ackDoc);
LinkedHashMap linkedHashMap = (LinkedHashMap)ackDoc.getDocRecord();
List<DemoTestPojo> pojoList = (List<DemoTestPojo>) linkedHashMap.get("demoTestPojoList");
System.out.println("pojoList = " +pojoList);
String cmt = (String) linkedHashMap.get("cmt");
System.out.println("cmt = " +cmt);
/* JsonNode jsonNode = mapper.readTree(json);
JsonNode doc_record = jsonNode.path("DOC_RECORD");
JsonNode demo_test = doc_record.path("DEMO_TEST");
String subJson = demo_test.toString();
List<DemoTestPojo> pojo = mapper.readValue(subJson, new TypeReference<List<DemoTestPojo>>() {});
System.out.println("pojo = " +pojo);*/
}
}
特殊
package org.example;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.example.pojo.AckDoc;
import org.example.pojo.AckDocHeader;
import org.example.pojo.DemoTestListPojo;
import org.example.pojo.DemoTestPojo;
import java.util.ArrayList;
import java.util.List;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws Exception {
List<DemoTestPojo> lst = new ArrayList<DemoTestPojo>();
for(int i=0 ; i<3 ; i++)
{
DemoTestPojo dt = new DemoTestPojo();
dt.setName("name" + i);
dt.setDes("des");
lst.add(dt);
}
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
DemoTestListPojo dtlp = new DemoTestListPojo();
dtlp.setDemoTestPojoList(lst);
dtlp.setCmt("wwww");
JsonNode rootNode = mapper.readTree(mapper.writeValueAsString(dtlp));
AckDocHeader ah = new AckDocHeader("sessionid-1","0","msg-empty");
AckDoc ack = new AckDoc(ah,rootNode);
// AckDoc to json jackjson
String json = mapper.writeValueAsString(ack);
System.out.println("json = " +json);
// json to AckDoc fastjson 添加了外层pojo,不能直接对象转json
AckDoc ackDoc = JSON.parseObject(json,AckDoc.class);
System.out.println("AckDoc = " +ackDoc);
JSONObject jsonObject = (JSONObject)ackDoc.getDocRecord();
DemoTestListPojo demoTestListPojo = jsonObject.toJavaObject(DemoTestListPojo.class);
List<DemoTestPojo> pojoList = demoTestListPojo.getDemoTestPojoList();
System.out.println("pojoList = " +pojoList);
String cmt = demoTestListPojo.getCmt();
System.out.println("cmt = " +cmt);
}
}
网友评论