星空网 > 软件开发 > ASP.net

java 把xml转化为json

java 中如何

常规思路是:

(1)通过第三方库 把

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

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

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

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

Java代码 java 把xml转化为json java 把xml转化为jsonjava 把xml转化为json

  1. new 
  2.         JSON jsonObj = 
  3.         String jsonStr = jsonObj.toString();  
  4.         jsonStr = jsonStr.replace("[]", "\"\"");  

 依赖:

Java代码 java 把xml转化为json java 把xml转化为jsonjava 把xml转化为json

  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 把

 

 

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

Java代码 java 把xml转化为json java 把xml转化为jsonjava 把xml转化为json

  1. package com.JSON_java;  
  2.   
  3. public class Main {  
  4.     public static int PRETTY_PRINT_INDENT_FACTOR = 4;  
  5.     public static String TEST_
  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 
  28.             String jsonPrettyPrintString = 
  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_

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

 

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

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

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

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

 

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

【java框架源码下载】




原标题:java 把xml转化为json

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

国家就国际商标的补贴政策实施情况进行综合评估:https://www.kjdsnews.com/a/1314007.html
国外商标注册:服务实力强,让你轻松出海:https://www.kjdsnews.com/a/1314008.html
国外品牌在国内申请商标注册:破解复杂的流程与细节:https://www.kjdsnews.com/a/1314009.html
国外品牌商标有什么新技术你必须知道!:https://www.kjdsnews.com/a/1314010.html
国内商标:唯一性是否有保障?:https://www.kjdsnews.com/a/1314011.html
商标申请:宣誓费用攻略:https://www.kjdsnews.com/a/1314012.html
凌晨三点到天津西站可以去哪里? :https://www.vstour.cn/a/410229.html
Temu广告投入上千万美元, 在美知名度高达88%!:https://www.kjdsnews.com/a/1842236.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流