分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

json串与xml串相互转换Utills

发布时间:2023-09-06 02:23责任编辑:熊小新关键词:jsjsonxml

一 、需要添加的maven依赖

          

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.11</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.11</version>
</dependency>

public class XmlJsonMain {
public static void main(String[] args) {
???????Map<String, String> map = new HashMap<>();
???????map.put("k1", "v1");
???????map.put("k2", "v2");

二、代码示例


???????//json串
???????String jsonStr = JSON.toJSONString(map);
???????System.out.println("source json : " + jsonStr);

???????//json转xml
???????String xml = json2xml(jsonStr);
???????System.out.println("xml ?: ?" + xml);
???????//xml转json
???????String targetJson = xml2json(xml);
???????System.out.println("target json : " + targetJson);
???}

???/**
????* json to xml
????* @param jsonStr
????* @return
????*/
???public static String json2xml(String jsonStr) {
???JSONObject jsonObj = new JSONObject(jsonStr);
???????return ?XML.toString(jsonObj);
???}

???/**
????* xml to json
????* @param xml
????* @return
????*/
???public static String xml2json(String xml) {
???
???
???????JSONObject xmlJSONObj = XML.toJSONObject(xml.replace("<xml>", "").replace("</xml>", ""));
???????return xmlJSONObj.toString();
???}

       

        

json串与xml串相互转换Utills

原文地址:https://www.cnblogs.com/xiaokong0824/p/9990447.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved