導航:首頁 > 編程語言 > 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相關的資料

熱點內容
網路中常用的傳輸介質 瀏覽:518
文件如何使用 瀏覽:322
同步推密碼找回 瀏覽:865
樂高怎麼才能用電腦編程序 瀏覽:65
本機qq文件為什麼找不到 瀏覽:264
安卓qq空間免升級 瀏覽:490
linux如何刪除模塊驅動程序 瀏覽:193
at89c51c程序 瀏覽:329
怎麼創建word大綱文件 瀏覽:622
裊裊朗誦文件生成器 瀏覽:626
1054件文件是多少gb 瀏覽:371
高州禁養區內能養豬多少頭的文件 瀏覽:927
win8ico文件 瀏覽:949
仁和數控怎麼編程 瀏覽:381
項目文件夾圖片 瀏覽:87
怎麼在東芝電視安裝app 瀏覽:954
plc顯示數字怎麼編程 瀏覽:439
如何辨別假網站 瀏覽:711
寬頻用別人的賬號密碼 瀏覽:556
新app如何佔有市場 瀏覽:42

友情鏈接