public class JsonTest {
private final Logger cLogger = Logger.getLogger(getClass());
/**
* XML转JSON
* 需import org.json.JSONObject; pom加依赖
*
*/
@Test
public void xmlToJson() {
String comId="123456";
JSONObject xmlJSONObj = org.json.XML.toJSONObject("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
"\t<PackageList>\n" +
"\t\t<Package>\n" +
"\t\t\t<Header>\n" +
"\t\t\t\t<Version>3.0.0</Version>\n" +
"\t\t\t\t<RequestType>03</RequestType>\n" +
"\t\t\t\t<UUID>14336376457645768578</UUID>\n" +
"\t\t\t\t<ComId>123456</ComId>\n" +
"\t\t\t\t<SendTime>2014-04-22 18:00:00</SendTime>\n" +
"\t\t\t\t<FromSerial>1234567890</FromSerial>\n" +
"\t\t\t\t<ProductCode>2222222</ProductCode>\n" +
"\t\t\t</Header>\n" +
"\t\t\t<Callback>\n" +
"\t\t\t\t<OrderId>2222222</OrderId>\n" +
"\t\t\t\t<PolicyNo>2222222666</PolicyNo>\n" +
"\t\t\t\t<RefundNo>2222222666</RefundNo>\n" +
"\t\t\t\t<RespondPrice>100</RespondPrice>\n" +
"\t\t\t\t<RealRefundPrice>100</RealRefundPrice>\n" +
"\t\t\t\t<HandingFee>10</HandingFee>\n" +
"\t\t\t\t<RefundTime>2014-04-22 18:00:00</RefundTime>\n" +
"\t\t\t\t<AuditTime>2014-04-22 18:00:00</AuditTime>\n" +
"\t\t\t\t<RefundChannel>保险公司</RefundChannel>\n" +
"\t\t\t\t<RefundStatus>1</RefundStatus>\n" +
"\t\t\t\t<RefundReslut>通过</RefundReslut>\n" +
"\t\t\t\t<BankCardNo>32445646458757766775</BankCardNo>\n" +
"\t\t\t\t<RefundUser>测试</RefundUser>\n" +
"\t\t\t\t<RefundTradeNo>12355464567768686786</RefundTradeNo>\n" +
"\t\t\t\t<RefundPremium>11</RefundPremium>\n" +
"\t\t\t\t<RefundType>1</RefundType>\n" +
"\t\t\t</Callback>\n" +
"\t\t</Package>\n" +
"\t</PackageList>");
cLogger.info("XML转换后的字符串JSON为:"+xmlJSONObj.toString());
cLogger.info("XML转换后的格式化JSON为:"+formatJson(xmlJSONObj.toString()));
}
/**
* JSON转XML
* 需import org.json.JSONObject;
* <dependency>
* <groupId>org.json</groupId>
* <artifactId>json</artifactId>
* <version>20180813</version>
* </dependency>
*
*
*
*/
@Test
public void jsonToXml() {
String json="{\n" +
" \"PackageList\": {\n" +
" \"Package\": {\n" +
" \"Header\": {\n" +
" \"Version\": \"3.0.0\",\n" +
" \"RequestType\": \"03\",\n" +
" \"UUID\": \"14336376457645768578\",\n" +
" \"ComId\": \"123456\",\n" +
" \"SendTime\": \"2014-04-22 18:00:00\",\n" +
" \"FromSerial\": \"1234567890\",\n" +
" \"ProductCode\": \"2222222\"\n" +
" },\n" +
" \"Callback\": {\n" +
" \"OrderId\": \"2222222\",\n" +
" \"PolicyNo\": \"2222222666\",\n" +
" \"RefundNo\": \"2222222666\",\n" +
" \"RespondPrice\": \"100\",\n" +
" \"RealRefundPrice\": \"100\",\n" +
" \"HandingFee\": \"10\",\n" +
" \"RefundTime\": \"2014-04-22 18:00:00\",\n" +
" \"AuditTime\": \"2014-04-22 18:00:00\",\n" +
" \"RefundChannel\": \"保险公司\",\n" +
" \"RefundStatus\": \"1\",\n" +
" \"RefundReslut\": \"通过\",\n" +
" \"BankCardNo\": \"32445646458757766775\",\n" +
" \"RefundUser\": \"测试\",\n" +
" \"RefundTradeNo\": \"12355464567768686786\",\n" +
" \"RefundPremium\": \"11\",\n" +
" \"RefundType\": \"1\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
JSONObject jsonObj = new JSONObject(json);
cLogger.info("JSON转换后的字符串XML为:"+XML.toString(jsonObj));
cLogger.info("JSON转换后的格式化XML为:"+forMateOutData(XML.toString(jsonObj)));
}
/**
* 获取JSON数据
* 需import com.alibaba.fastjson.JSONObject;
*/
@Test
public void getJsonText(){
String params="{\n" +
" \"PackageList\": {\n" +
" \"Package\": {\n" +
" \"Header\": {\n" +
" \"Version\": \"3.0.0\",\n" +
" \"RequestType\": \"03\",\n" +
" \"UUID\": \"14336376457645768578\",\n" +
" \"ComId\": \"123456\",\n" +
" \"SendTime\": \"2014-04-22 18:00:00\",\n" +
" \"FromSerial\": \"1234567890\",\n" +
" \"ProductCode\": \"2222222\"\n" +
" },\n" +
" \"Callback\": {\n" +
" \"OrderId\": \"2222222\",\n" +
" \"PolicyNo\": \"2222222666\",\n" +
" \"RefundNo\": \"2222222666\",\n" +
" \"RespondPrice\": \"100\",\n" +
" \"RealRefundPrice\": \"100\",\n" +
" \"HandingFee\": \"10\",\n" +
" \"RefundTime\": \"2014-04-22 18:00:00\",\n" +
" \"AuditTime\": \"2014-04-22 18:00:00\",\n" +
" \"RefundChannel\": \"保险公司\",\n" +
" \"RefundStatus\": \"1\",\n" +
" \"RefundReslut\": \"通过\",\n" +
" \"BankCardNo\": \"32445646458757766775\",\n" +
" \"RefundUser\": \"测试\",\n" +
" \"RefundTradeNo\": \"12355464567768686786\",\n" +
" \"RefundPremium\": \"11\",\n" +
" \"RefundType\": \"1\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
com.alibaba.fastjson.JSONObject jsonObject =com.alibaba.fastjson.JSONObject.parseObject(params);
com.alibaba.fastjson.JSONObject aPackageList=com.alibaba.fastjson.JSONObject.parseObject(jsonObject.get("PackageList").toString());
com.alibaba.fastjson.JSONObject aPackage=com.alibaba.fastjson.JSONObject.parseObject(aPackageList.get("Package").toString());
com.alibaba.fastjson.JSONObject aHeader=com.alibaba.fastjson.JSONObject.parseObject(aPackage.get("Header").toString());
com.alibaba.fastjson.JSONObject aCallback=com.alibaba.fastjson.JSONObject.parseObject(aPackage.get("Callback").toString());
cLogger.info(aHeader.get("Version").toString());
cLogger.info(aCallback.get("RefundChannel").toString());
cLogger.info(aCallback.get("RefundTime").toString());
}
/**
* 拼接JSON字符串
*/
@Test
public void appendStringJson(){
StringBuilder sb = new StringBuilder();
String conId="123456";
sb.append("{");
sb.append("\"PackageList\":").append("{");
sb.append("\"Package\":").append("{");
sb.append("\"Header\":").append("{");
sb.append("\"ComId\":").append("\""+conId+"\"");
sb.append(",\"FromSerial\":").append("\"1234567890\"");
sb.append(",\"ProductCode\":").append("\"2222222\"");
sb.append(",\"SendTime\":").append("\"2014-04-22 18:00:00\"");
sb.append(",\"UUID\":").append("\"14336376457645768578\"");
sb.append(",\"Version\":").append("\"3.0.0\"");
sb.append(",\"RequestType\":").append("\"03\"");
sb.append("}");
sb.append(",\"Callback\":").append("{");
sb.append("\"RefundTradeNo\":").append("\"12355464567768686786\"");
sb.append(",\"RefundTime\":").append("\"2014-04-22 18:00:00\"");
sb.append(",\"RefundStatus\":").append("\"1\"");
sb.append(",\"OrderId\":").append("\"2222222\"");
sb.append(",\"PolicyNo\":").append("\"2222222666\"");
sb.append(",\"RefundReslut\":").append("\"通过\"");
sb.append(",\"HandingFee\":").append("\"10\"");
sb.append(",\"RefundUser\":").append("\"测试\"");
sb.append(",\"BankCardNo\":").append("\"32445646458757766775\"");
sb.append(",\"RefundNo\":").append("\"2222222666\"");
sb.append(",\"AuditTime\":").append("\"2014-04-22 18:00:00\"");
sb.append(",\"RefundChannel\":").append("\"保险公司\"");
sb.append(",\"RefundPremium\":").append("\"11\"");
sb.append(",\"RealRefundPrice\":").append("\"100\"");
sb.append(",\"RespondPrice\":").append("\"100\"");
sb.append(",\"RefundType\":").append("\"1\"");
sb.append("}");
sb.append("}");
sb.append("}");
sb.append("}");
cLogger.info(sb.toString());
cLogger.info(formatJson(sb.toString()));
}
/**
* 格式化输出JSON报文
*
* @param jsonStr
* @return
* @author lcb
*/
public static String formatJson(String jsonStr) {
if (null == jsonStr || "".equals(jsonStr))
return "";
StringBuilder sb = new StringBuilder();
char last = '\0';
char current = '\0';
int indent = 0;
boolean isInQuotationMarks = false;
for (int i = 0; i < jsonStr.length(); i++) {
last = current;
current = jsonStr.charAt(i);
switch (current) {
case '"':
if (last != '\\'){
isInQuotationMarks = !isInQuotationMarks;
}
sb.append(current);
break;
case '{':
case '[':
sb.append(current);
if (!isInQuotationMarks) {
sb.append('\n');
indent++;
addIndentBlank(sb, indent);
}
break;
case '}':
case ']':
if (!isInQuotationMarks) {
sb.append('\n');
indent--;
addIndentBlank(sb, indent);
}
sb.append(current);
break;
case ',':
sb.append(current);
if (last != '\\' && !isInQuotationMarks) {
sb.append('\n');
addIndentBlank(sb, indent);
}
break;
default:
sb.append(current);
}
}
return sb.toString();
}
/**
* 添加space
*
* @param sb
* @param indent
* @author lcb
*/
private static void addIndentBlank(StringBuilder sb, int indent) {
for (int i = 0; i < indent; i++) {
sb.append('\t');
}
}
/**
* 格式话输出XML报文.
*
* @param outXml
* @return
*/
private String forMateOutData(String outXml) {
Pattern p = Pattern.compile("\r|\n");
Matcher m = p.matcher(outXml);
outXml = m.replaceAll("");
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(format);
return outputter.outputString(JdomUtil.build(outXml.trim()));
}
}
转载于:https://www.cnblogs.com/slimshady/p/9790886.html