日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

json报文转化为xml报文_JSON与XML互相转化(Jackson)

發布時間:2024/9/15 asp.net 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 json报文转化为xml报文_JSON与XML互相转化(Jackson) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

選型: json-lib.jar可以做,但是太老了,拋棄; fastjson完全沒有xml相關功能; staxon經測試不能用。 最終選用jackson來主要實現。

JDK為1.8,如果低于1.8,則只需要將try-catch的語法改回傳統書寫方式。

目前的實現,傳參和回參都是String,如果想JSONObject對象和XML對象互轉,只需要再包一層就可以多2個或4個方法。

一、依賴

pom.xml

com.fasterxml.jackson.core

jackson-core

2.8.8

com.fasterxml.jackson.core

jackson-databind

2.8.8

com.fasterxml.jackson.dataformat

jackson-dataformat-xml

2.8.8

com.alibaba

fastjson

1.2.33

二、實現

package com.kapu.common.utility;

import java.io.IOException;

import java.io.StringWriter;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.JsonParseException;

import com.fasterxml.jackson.core.JsonParser;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class JsonXmlUtil {

private static final String XML_PREFIX = "<?xml version="1.0" encoding="UTF-8" ?>";

private static final String XML_PREFIX_HEAD = "";

private static final String XML_PREFIX_TAIL = "?>";

private static final String XML_ROOT_START = "";

private static final String XML_ROOT_END = "";

/**

* json string convert to xml string

* @throws IOException

* @throws JsonParseException

*/

public static String json2xml(String json) throws IOException {

if (StringUtils.isEmpty(json)) {

return null;

}

JSONObject jsonObject = JSON.parseObject(json);

StringWriter w = new StringWriter();

XmlMapper xmlMapper = new XmlMapper();

xmlMapper.writeValue(w, jsonObject);

String xml = w.toString();

xml = xml.replaceAll("", "").replaceAll("", "");

xml = XML_PREFIX + xml;

return xml;

}

/**

* xml string convert to json string

* @throws IOException

*/

public static String xml2json(String xml) throws IOException {

if(StringUtils.isEmpty(xml)) {

return null;

}

if(xml.indexOf(XML_PREFIX_HEAD) > -1 && xml.indexOf(XML_PREFIX_TAIL) > -1){

xml = xml.substring(xml.indexOf(XML_PREFIX_TAIL) + 2);

}

xml = XML_ROOT_START + xml + XML_ROOT_END;

StringWriter w = new StringWriter();

try(JsonParser jp = new XmlMapper().getFactory().createParser(xml);JsonGenerator jg = new ObjectMapper().getFactory().createGenerator(w);){

while (jp.nextToken() != null) {

jg.copyCurrentEvent(jp);

}

}

return w.toString();

}

}

三、測試

package com.kapu.common.base;

import java.io.IOException;

import org.junit.Test;

import com.kapu.common.utility.JsonXmlUtil;

import com.alibaba.fastjson.JSONObject;

public class JsonXml {

@Test

public void testJson2Xml() {

JSONObject meta = new JSONObject();

meta.put("from", "WEB");

meta.put("token", "sdfa4sdf4as5d4f8a4sd8f4asdf");

meta.put("timestamp", System.currentTimeMillis());

JSONObject data = new JSONObject();

data.put("id", 123);

data.put("name", "one-two-three");

data.put("desc", "this is a number list");

JSONObject json = new JSONObject();

json.put("meta", meta);

json.put("data", data);

json.put("aaa", "aaaaaa");

System.out.println(json);

String xml = null;

try {

xml = JsonXmlUtil.json2xml(json.toJSONString());

} catch (Throwable e) {

e.printStackTrace();

}

System.out.println(xml);

}

@Test

public void testXml2Json() {

String xml = "<?xml version="1.0" encoding="UTF-8" ?>aaaaaaone-two-three123thisisanumberlistWEBsdfa4sdf4as5d4f8a4sd8f4asdf1536049559684";

try {

String json = JsonXmlUtil.xml2json(xml);

System.out.println(json);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Test

public void testJson2Xml2Json() throws IOException {

JSONObject meta = new JSONObject();

meta.put("from", "WEB");

meta.put("token", "sdfa4sdf4as5d4f8a4sd8f4asdf");

meta.put("timestamp", System.currentTimeMillis());

JSONObject data = new JSONObject();

data.put("id", 123);

data.put("name", "one-two-three");

data.put("desc", "this is a number list");

JSONObject json = new JSONObject();

json.put("meta", meta);

json.put("data", data);

json.put("aaa", "aaaaaa");

System.out.println(json.toJSONString());

String xml = JsonXmlUtil.json2xml(json.toJSONString());

System.out.println(xml);

String jsonStr = JsonXmlUtil.xml2json(xml);

System.out.println(jsonStr);

}

}

總結

以上是生活随笔為你收集整理的json报文转化为xml报文_JSON与XML互相转化(Jackson)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。