JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(网络传输速度)。XML它可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。 它非常适合万维网传输,提供统一的方法来描述和交换独立于应用程序或供应商的结构化数据。是Internet环境中跨平台的、依赖于内容的技术,也是当今处理分布式结构信息的有效工具。早在1998年,W3C就发布了XML1.0规范,使用它来简化Internet的文档信息传输。作为开发中比较常用到的两种数据格式,他们之间的转换是怎样的呢,下面就是关于它们转化的工具类。
一: 本实例所需jar包
maven 中的依赖
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk13</classifier>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.2</version>
</dependency>
测试文件gao.json
[
{
"name": "司马长风",
"age": "25",
"add": "四方城",
"gender": "男"
},
{
"name": "上官燕",
"age": "23",
"add": "凤凰都",
"gender": "女"
},
{
"name": "皇甫仁和",
"age": "26",
"add": "四方城",
"gender": "男"
},
{
"name": "欧阳明日",
"age": "24",
"add": "飞鹰堡",
"gender": "男"
}
]
XmlJSONUtil.java类代码
package com.test.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
/**
* json xml 互转工具类
* @author:yqgao
* @date:2015-4-16 下午02:01:38
*/
public class XmlJSONUtil {
private static XMLSerializer xmlserializer = new XMLSerializer();
private static final String STR_JSON_OBJECT = "{"+
"\"name\": \"小王\","+
"\"age\": \"20\","+
"\"add\": \"北京\","+
"\"gender\": \"男\""+
"}";
private static final String STR_JSON_ARRAY = "[{"+
"\"name\": \"小王\","+
"\"age\": \"20\","+
"\"add\": \"北京\","+
"\"gender\": \"男\""+
"},"+
"{"+
"\"name\": \"小李\","+
"\"age\": \"21\","+
"\"add\": \"上海\","+
"\"gender\": \"女\""+
"}]";
/**
* xml格式字符串转化成jsonObject或者jsonArray
* @param xml
* @return
*/
public static String xml2json(String xml){
String rs = "";
try {
rs = xmlserializer.read(xml).toString();
} catch (Exception e) {
//e.printStackTrace();
System.err.println("xml转化为json异常...");
}
return rs;
}
/**
* jsonArray或者jsonObject字符串转化为xml
* @param json
* @return
*/
public static String json2xml(String json){
String rs = "";
try {
if(json.contains("[")&&json.contains("]")){
//jsonArray
JSONArray jobj = JSONArray.fromObject(json);
rs = xmlserializer.write(jobj);
}else{
//jsonObject
JSONObject jobj = JSONObject.fromObject(json);
rs = xmlserializer.write(jobj);
}
} catch (Exception e) {
//e.printStackTrace();
System.err.println("jsonArray转化为xml异常...");
}
return rs;
}
/***
* json和xml文件互转
* @param sourcePath json文件的路径
* @param outPath xml文件的路径
* @param enterFlag 转化标识 1表示json转化为xml
* 2表示xml转化为json
* @return
*/
public static String jfxfTranspose(String sourcePath,String outPath,int enterFlag){
FileInputStream in =null;
BufferedReader br = null;
FileWriter fw = null;
String rs = null;
try{
File jsonFile = new File(sourcePath);
in = new FileInputStream(jsonFile);
StringBuffer sbuf = new StringBuffer();
br = new BufferedReader(new InputStreamReader(in));
String temp =null;
while((temp=br.readLine())!=null){
sbuf.append(temp);
}
if(1==enterFlag){
rs = json2xml(sbuf.toString());
}else{
rs = xml2json(sbuf.toString());
}
File test = new File(outPath);
if(!test.exists()){
test.createNewFile();
}
fw = new FileWriter(test);
fw.write(rs);
}catch (Exception e) {
System.err.println("json和xml转化文件异常...");
}finally{
try {
fw.close();
br.close();
in.close();
} catch (Exception e) {
System.err.println("输入、输出流关闭异常");
e.printStackTrace();
}
}
return rs;
}
public static void main(String[] args) throws IOException {
// String xml1 = json2xml(STR_JSON_OBJECT);
String xml1 = json2xml(STR_JSON_ARRAY);
System.out.println("xml ==>"+xml1);
String json1 = xml2json(xml1);
System.out.println("json==>"+json1);
String spath2 = "F:/WorkSpace/JavaProject/src/com/test/util/gao.json";
String opath2 = "F:/WorkSpace/JavaProject/src/com/test/util/testGao.xml";
String rs2 = jfxfTranspose(spath2,opath2,1);
System.out.print(rs2);
String spath1 = "F:/WorkSpace/JavaProject/src/com/test/util/testGao.xml";
String opath1 = "F:/WorkSpace/JavaProject/src/com/test/util/test.json";
String rs1 = jfxfTranspose(spath1,opath1,2);
System.out.print(rs1);
}
}
以上代码中的路径,请以自己电脑路径为准。
运行代码后可以看到控制台的输出和项目目录下生成的文件如下
控制台
xml ==><?xml version="1.0" encoding="UTF-8"?>
<a><e class="object"><add type="string">北京</add><age type="string">20</age><gender type="string">男</gender><name type="string">小王</name></e><e class="object"><add type="string">上海</add><age type="string">21</age><gender type="string">女</gender><name type="string">小李</name></e></a>
2015-4-17 16:22:05 net.sf.json.xml.XMLSerializer getType
信息: Using default type string
json==>[{"add":"北京","age":"20","gender":"男","name":"小王"},{"add":"上海","age":"21","gender":"女","name":"小李"}]
<?xml version="1.0" encoding="UTF-8"?>
<a><e class="object"><add type="string">四方城</add><age type="string">25</age><gender type="string">男</gender><name type="string">司马长风</name></e><e class="object"><add type="string">凤凰都</add><age type="string">23</age><gender type="string">女</gender><name type="string">上官燕</name></e><e class="object"><add type="string">四方城</add><age type="string">26</age><gender type="string">男</gender><name type="string">皇甫仁和</name></e><e class="object"><add type="string">飞鹰堡</add><age type="string">24</age><gender type="string">男</gender><name type="string">欧阳明日</name></e></a>
2015-4-17 16:22:05 net.sf.json.xml.XMLSerializer getType
信息: Using default type string
[{"add":"四方城","age":"25","gender":"男","name":"司马长风"},{"add":"凤凰都","age":"23","gender":"女","name":"上官燕"},{"add":"四方城","age":"26","gender":"男","name":"皇甫仁和"},{"add":"飞鹰堡","age":"24","gender":"男","name":"欧阳明日"}]
生成的文件
testGao.xml
<?xml version="1.0" encoding="UTF-8"?>
<a>
<e class="object">
<add type="string">四方城</add>
<age type="string">25</age>
<gender type="string">男</gender>
<name type="string">司马长风</name>
</e>
<e class="object">
<add type="string">凤凰都</add>
<age type="string">23</age>
<gender type="string">女</gender>
<name type="string">上官燕</name>
</e>
<e class="object">
<add type="string">四方城</add>
<age type="string">26</age>
<gender type="string">男</gender>
<name type="string">皇甫仁和</name>
</e>
<e class="object">
<add type="string">飞鹰堡</add>
<age type="string">24</age>
<gender type="string">男</gender>
<name type="string">欧阳明日</name>
</e>
</a>
test.json
[
{
"add": "四方城",
"age": "25",
"gender": "男",
"name": "司马长风"
},
{
"add": "凤凰都",
"age": "23",
"gender": "女",
"name": "上官燕"
},
{
"add": "四方城",
"age": "26",
"gender": "男",
"name": "皇甫仁和"
},
{
"add": "飞鹰堡",
"age": "24",
"gender": "男",
"name": "欧阳明日"
}
]
以上就是xml和json相互转化的小尝试,完整项目代码链接:http://download.csdn.net/detail/gao36951/8604519
时间: 2024-10-13 09:53:27