java 把xml转化为json

java 中如何把xml转化为json 呢?

常规思路是:

(1)通过第三方库 把xml 转换为java bean;

(2)把java bean 序列化为json 字符串

但是上述方式有一个缺点,那就是需要java bean来中转.

以下提供两种方式 不需要java bean

方式一:使用json-lib java 把xml转化为json  

Java代码 复制代码  收藏代码
  1. XMLSerializer xmlSerializer = new XMLSerializer();  
  2.         JSON jsonObj = xmlSerializer.read(responseTextObj);  
  3.         String jsonStr = jsonObj.toString();  
  4.         jsonStr = jsonStr.replace("[]", "\"\"");  
XMLSerializer xmlSerializer = new XMLSerializer();

		JSON jsonObj = xmlSerializer.read(responseTextObj);

		String jsonStr = jsonObj.toString();

		jsonStr = jsonStr.replace("[]", "\"\"");

 依赖:

Java代码 复制代码  收藏代码
  1. <dependency>  
  2.             <groupId>net.sf.json-lib</groupId>  
  3.             <artifactId>json-lib</artifactId>  
  4.             <version>2.4</version>  
  5.             <classifier>jdk15</classifier>  
  6.         </dependency>  
  7.         <dependency>  
  8.             <groupId>xom</groupId>  
  9.             <artifactId>xom</artifactId>  
  10.             <version>1.2.5</version>  
  11.         </dependency>  
  12.   
  13.         <dependency>  
  14.             <groupId>xom</groupId>  
  15.             <artifactId>xom</artifactId>  
  16.             <version>1.2.5</version>  
  17.             <classifier>sources</classifier>  
  18.         </dependency>  
<dependency>

            <groupId>net.sf.json-lib</groupId>

            <artifactId>json-lib</artifactId>

            <version>2.4</version>

            <classifier>jdk15</classifier>

        </dependency>

        <dependency>

            <groupId>xom</groupId>

            <artifactId>xom</artifactId>

            <version>1.2.5</version>

        </dependency>



        <dependency>

            <groupId>xom</groupId>

            <artifactId>xom</artifactId>

            <version>1.2.5</version>

            <classifier>sources</classifier>

        </dependency>

 注意:通过json-lib 把xml转化为json时,空节点都会转化为空数组,即[],这是非常不好的,所以需要把[]转化为空字符串:jsonStr.replace("[]", "\"\"")

 

 

方式二:使用github 上开源的库

Java代码 复制代码  收藏代码
  1. package com.JSON_java;  
  2.   
  3. public class Main {  
  4.     public static int PRETTY_PRINT_INDENT_FACTOR = 4;  
  5.     public static String TEST_XML_STRING =  
  6.             "<breakfast_menu>\n" +  
  7.                     "<food>\n" +  
  8.                     "<name>Belgian Waffles</name>\n" +  
  9.                     "<price>$5.95</price>\n" +  
  10.                     "<description>\n" +  
  11.                     "Two of our famous Belgian Waffles with plenty of real maple syrup\n" +  
  12.                     "</description>\n" +  
  13.                     "<calories>650</calories>\n" +  
  14.                     "</food>\n" +  
  15.                     "<food>\n" +  
  16.                     "<name>Strawberry Belgian Waffles</name>\n" +  
  17.                     "<price>$7.95</price>\n" +  
  18.                     "<description>\n" +  
  19.                     "Light Belgian waffles covered with strawberries and whipped cream\n" +  
  20.                     "</description>\n" +  
  21.                     "<calories>900</calories>\n" +  
  22.                     "</food>\n" +  
  23.                     "</breakfast_menu>";  
  24.   
  25.     public static void main(String[] args) {  
  26.         try {  
  27.             JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);  
  28.             String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);  
  29.             System.out.println(jsonPrettyPrintString);  
  30.         } catch (JSONException je) {  
  31.             System.out.println(je.toString());  
  32.         }  
  33.     }  
  34. }  
package com.JSON_java;



public class Main {

	public static int PRETTY_PRINT_INDENT_FACTOR = 4;

    public static String TEST_XML_STRING =

            "<breakfast_menu>\n" +

                    "<food>\n" +

                    "<name>Belgian Waffles</name>\n" +

                    "<price>$5.95</price>\n" +

                    "<description>\n" +

                    "Two of our famous Belgian Waffles with plenty of real maple syrup\n" +

                    "</description>\n" +

                    "<calories>650</calories>\n" +

                    "</food>\n" +

                    "<food>\n" +

                    "<name>Strawberry Belgian Waffles</name>\n" +

                    "<price>$7.95</price>\n" +

                    "<description>\n" +

                    "Light Belgian waffles covered with strawberries and whipped cream\n" +

                    "</description>\n" +

                    "<calories>900</calories>\n" +

                    "</food>\n" +

                    "</breakfast_menu>";



    public static void main(String[] args) {

        try {

            JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);

            String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);

            System.out.println(jsonPrettyPrintString);

        } catch (JSONException je) {

            System.out.println(je.toString());

        }

    }

}

实际应用: java 把xml转化为json  

 

依赖的源码见附件 ,上述代码见附件中的Main.java

github 地址:https://github.com/douglascrockford/JSON-java

https://github.com/douglascrockford/JSON-java/blob/master/XML.java

参考:http://heshans.blogspot.com/2014/01/java-library-to-convert-xml-to-json.html

 

java企业级通用权限安全框架源码 SpringMVC mybatis or hibernate+ehcache shiro druid bootstrap HTML5

【java框架源码下载】

你可能感兴趣的:(xml转化为json)