XML格式转JSON格式

XML格式转JSON格式,第1张

XML格式转JSON格式

此方法导入org.json的jar包

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

public class XmlToJson {

    public static void main(String[] args) {
       String xml = "zhaipuhongmale19701217";
       String json = xmlStringToJson(xml);
       System.out.println(json);
    }

   
    public static String xmlStringToJson(String xmlString) {
    //判断字符串是否为空
    if(xmlString == null || "".equals(xmlString)){
    return "";
    }
       String json = null;
       JSONObject jsonObj = null;
       try {
           jsonObj = XML.toJSONObject(xmlString);
           json = jsonObj.toString(2);       //缩进
       } catch (JSONException e) {
          e.printStackTrace();
       }
       return json;
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5677917.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存