導航:首頁 > 編程語言 > fastjsonxml

fastjsonxml

發布時間:2023-03-29 01:29:10

『壹』 如何將xml的字元串轉換為json

放上maven依賴

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>

xml字元串轉換為json字元串代碼實現如下:

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

public class JsonUtils {
public static String xml2jsonString(String xml)throws JSONException{
JSONObject xmlJSONObj = XML.toJSONObject(xml);
return xmlJSONObj.toString();
}
}

json字元串轉換為json對象代碼實現如下:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

JSONObject jsonObject = null;
try{
jsonObject = JSON.parseObject(jsonString);
}catch(Exception e){
logger.error("json串轉json對象時發生異常,json串="+jsonString);
throw new RuntimeException("json串轉json對象時發生異常,json串="+jsonString,e);
}
return jsonObject;

不過org.json有個問題比較麻煩,xml如下所示:
<?xml version="1.0" encoding="gb2312"?>
<payUnitinfo>
<err_msg></err_msg>
<retcode>1</retcode>
<payUnits>
<payUnit>
<provinceId>v1955</provinceId>
<cityId>v1961</cityId>
<payProjectId>c2680</payProjectId>
<payUnitId>v96564</payUnitId>
<payUnitName>浙江省電費</payUnitName>
</payUnit>
<payUnit>
<provinceId>v1955</provinceId>
<cityId>v1961</cityId>
<payProjectId>c2680</payProjectId>
<payUnitId>v85002</payUnitId>
<payUnitName>國網浙江省電力公司</payUnitName>
</payUnit>
</payUnits>
</payUnitinfo>

當xml中出現多個<payUnit>時,org.json將xml中的<payUnit>轉換為json數組格式的字元串,當只有一個<payUnit>時,org.json將xml中的<payUnit>轉換為json對象格式的字元串。
假設jsonObject是上述xml轉換為json對象後的變數,則通過以下方式獲取:

JSONObject payUnitsJsonObject = jsonObject.getJSONObject("payUnitinfo").getJSONObject("payUnits");

JSONObject payUnitJsonObject = null;
JSONArray payUnitJsonArray = null;
Object payUnitObject = payUnitsJsonObject.get("payUnit");
if(payUnitObject instanceof JSONObject){
payUnitJsonObject = (JSONObject)payUnitObject;
}else if(payUnitObject instanceof JSONArray){
payUnitJsonArray = (JSONArray)payUnitObject;
}

『貳』 怎麼將json文件轉成xml文件

json的話,目前比較流行的轉換工具是fastjson,使用方便,速度又很快。。可版以在javabean和string型之間直接轉權換
xml的話,可以採用xstream來轉換。。。
也就是說,你可以先把json的文件內容轉換成javabean(fastjson),然後再把javabean轉換成xml(xstream)
打字不易,如滿意,望採納。

『叄』 Java 中解析 XML 生成 Bean 實例,用什麼方式效率最高

用org.json以及谷歌提供gson來解析json數據的方式更好一些。

安卓下通常採用以下幾種方式解析json數據:
1、org.json包(已經集成到android.jar中了)
2、google提供的gson庫
3、阿里巴巴的fastjson庫
4、json-lib

以Google出品的Gson為例,具體步驟為:
1、首先,從 code.google.com/p/google-gson/downloads/list下載GsonAPI:
google-gson-1.7.1-release.zip 把gson-1.7.jar 到libs(項目根目錄新建一個libs文件夾)中。 可以使用以下兩種方法解析JSON數據,通過獲取JsonReader對象解析JSON數據。
代碼如下:
String jsonData = "[{\"username\":\"arthinking\",\"userId\":001},{\"username\":\"Jason\",\"userId\":002}]";
try{
JsonReader reader = new JsonReader(new StringReader(jsonData));
reader.beginArray();
while(reader.hasNext()){
reader.beginObject();
while(reader.hasNext()){
String tagName = reader.nextName();
if(tagName.equals("username")){
System.out.println(reader.nextString());
}
else if(tagName.equals("userId")){
System.out.println(reader.nextString());
}
}
reader.endObject();
}
reader.endArray();
}
catch(Exception e){
e.printStackTrace();
}
2、使用Gson對象獲取User對象數據進行相應的操作:
代碼如下:

Type listType = new TypeToken<LinkedList<User>>(){}.getType();
Gson gson = new Gson();
LinkedList<User> users = gson.fromJson(jsonData, listType);
for (Iterator iterator = users.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
System.out.println(user.getUsername());
System.out.println(user.getUserId());
}
3、如果要處理的JSON字元串只包含一個JSON對象,則可以直接使用fromJson獲取一個User對象:
代碼如下:

String jsonData = "{\"username\":\"arthinking\",\"userId\":001}";
Gson gson = new Gson();
User user = gson.fromJson(jsonData, User.class);
System.out.println(user.getUsername());
System.out.println(user.getUserId());

閱讀全文

與fastjsonxml相關的資料

熱點內容
為什麼淘寶app是黑色的 瀏覽:17
如何在cad中把圖形輸出為pdf文件 瀏覽:535
文件夾橫簽 瀏覽:988
extjs5mvc 瀏覽:614
win7如何安裝資料庫 瀏覽:647
informix資料庫倒數卸數 瀏覽:983
華碩p7h55mplus升級 瀏覽:240
servlet調用jsp 瀏覽:481
文件的命名原則有哪些 瀏覽:352
蘋果的文件管理是哪個 瀏覽:387
智能黑板如何給pdf文件做批註 瀏覽:788
哈弗智聯app如何綁定二手車 瀏覽:728
cad文件不多可是異常增大 瀏覽:872
蘋果手機怎樣將音頻文件導入剪映 瀏覽:432
2016秋季飛歌導航升級 瀏覽:151
電腦字元串怎麼編程 瀏覽:381
暴風不能在線觀看視頻文件 瀏覽:267
三國卡可以升級嗎 瀏覽:939
如何篩選出相同數據 瀏覽:311
vbox文件找不到 瀏覽:49

友情鏈接