【Java123】XML与JSON互相转化

2020-12-13 04:50

阅读:475

标签:except   gui   ret   sed   需要   lin   res   turn   scl   

 http://www.bejson.com/json2javapojo/new/

https://www.cnblogs.com/111testing/p/9162229.html

https://wenku.baidu.com/view/8364d04b86c24028915f804d2b160b4e777f8102.html

https://www.jianshu.com/p/8dadd9a232b1

 

 

一个使用org.json的Demo:

技术图片技术图片
 1 import org.apache.commons.io.IOUtils;
 2 import org.json.JSONException;
 3 import org.json.JSONObject;
 4 import org.json.XML;
 5 
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 
 9 public class XML2JSON {
10 
11     public XML2JSON(){}
12 
13     public static String xml2jsonString(String  filepath) throws IOException, JSONException {
14 
15         InputStream in = XML2JSON.class.getResourceAsStream(filepath);
16         String xml = IOUtils.toString(in);
17         System.out.print(xml);
18         System.out.println();
19 
20         JSONObject xmlJSONObj = XML.toJSONObject(xml);
21         String jsonStr = xmlJSONObj.toString();
22         System.out.print(jsonStr);
23 
24         return xmlJSONObj.toString();
25 
26     }
27 
28 
29     public static void main(String[] args) throws JSONException, IOException {
30 
31         String string = xml2jsonString("simple.xml");
32         System.out.println(string);
33 
34     }
35 
36 }
View Code

测试1:

 
  Gambardella, Matthew
  XML Developer‘s Guide
  Computer
  44.95
  2000-10-01
  An in-depth look at creating applications
   with XML.

 
 
 Ralls, Kim
 Midnight Rain
 NOAA‘s National Weather Service
 http://weather.gov/
 
 
  http://weather.gov/images/xml_logo.gif
  NOAA‘s National Weather Service
  http://weather.gov
 
 New York/John F. Kennedy Intl Airport, NY
 KJFK
 40.66
 -73.78
 
  Mon, 11 Feb 2008 06:51:00 -0500 EST
 
 A Few Clouds
 11
 -12
 36
 West
 280
 18.4
 29
 1023.6
 30.23
 -11
 -24
 -7
 -22
 10.00
 
  http://weather.gov/weather/images/fcicons/
 

 nfew.jpg
 
  http://www.weather.gov/data/obhistory/KJFK.html
 

 
  http://weather.gov/disclaimer.html
 

 
  http://weather.gov/disclaimer.html
 
 
{"current_observation":{"copyright_url":"http://weather.gov/disclaimer.html","windchill_f":-7,"windchill_c":-22,"book":[{"author":"Gambardella, Matthew","price":44.95,"genre":"Computer","description":"An in-depth look at creating applications\r\n\t\t\twith XML.","id":"bk101","title":"XML Developer‘s Guide","publish_date":"2000-10-01"},{"author":"Ralls, Kim","credit_URL":"http://weather.gov/","id":"bk102","title":"Midnight Rain","credit":"NOAA‘s National Weather Service"}],"latitude":40.66,"two_day_history_url":"http://www.weather.gov/data/obhistory/KJFK.html","temp_c":-12,"temp_f":11,"wind_mph":18.4,"wind_degrees":280,"icon_url_base":"http://weather.gov/weather/images/fcicons/","icon_url_name":"nfew.jpg","weather":"A Few Clouds","dewpoint_f":-11,"longitude":-73.78,"image":{"link":"http://weather.gov","title":"NOAA‘s National Weather Service","url":"http://weather.gov/images/xml_logo.gif"},"dewpoint_c":-24,"station_id":"KJFK","wind_dir":"West","pressure_in":30.23,"wind_gust_mph":29,"disclaimer_url":"http://weather.gov/disclaimer.html","location":"New York/John F. Kennedy Intl Airport, NY","relative_humidity":36,"pressure_mb":1023.6,"visibility_mi":10,"observation_time_rfc822":"Mon, 11 Feb 2008 06:51:00 -0500 EST"}}

 

测试2:



 
 
 
 Hello World!
 
  Helen
  Jason
 

 
  Gambardella, Matthew
  XML Developer‘s Guide
  Computer
  44.95
  2000-10-01
  An in-depth look at creating applications
   with XML.

 
 
 Ralls, Kim
 Midnight Rain
 NOAA‘s National Weather Service
 http://weather.gov/
 

{"current_observation":{"book":["","",{"id":"bk98"},{"id":"bk99","content":"Hello World!"},{"author":["Helen","Jason"],"id":"bk100"},{"author":"Gambardella, Matthew","price":44.95,"genre":"Computer","description":"An in-depth look at creating applications\r\n\t\t\twith XML.","id":"bk101","title":"XML Developer‘s Guide","publish_date":"2000-10-01"},{"author":"Ralls, Kim","credit_URL":"http://weather.gov/","id":"bk102","title":"Midnight Rain","credit":"NOAA‘s National Weather Service"}]}}

 

 

各路方法,有使用第三方API,或者自写遍历迭代方法。

 

在针对上述Demo的测试结果做分析,还需要进一步考虑以下几点:

1. 源XML文件自身特殊及复杂情况的转换定义。比如,字段含特殊字符,一个标签含多个属性,多个标签同名,invalid file等。

2. 目标JSON文件的转换定义。比如,各元素是否保持原有顺序,多个同名标签作为数组还是同级对象,等。

3. 转换过程中的异常处理。比如,转换中遇到无法解析而跳出导致该目标文件无效,异常捕获记录,等。

 

 

 

https://blog.csdn.net/bestxiaok/article/details/80443640

 

【Java123】XML与JSON互相转化

标签:except   gui   ret   sed   需要   lin   res   turn   scl   

原文地址:https://www.cnblogs.com/cathygx/p/10596964.html


评论


亲,登录后才可以留言!