首页 > 编程知识 正文

jsonobject数据格式,对象转成jsonobject

时间:2023-05-03 11:56:25 阅读:180291 作者:1487

我在网上找了很多资料,要么只能转jsonobject,要么只能转jsonarray。 以下是可以通过两种混合方式旋转的工具类

ependencygroupidorg.dom4j/groupidartifactiddom 4j/artifactidversion2.1.1/version/dependency代码部分

package com.xing yi.RSC mgr.utils.conversion; import com.Alibaba.fast JSON.JSON; import com.Alibaba.fast JSON.JSON array; import com.Alibaba.fast JSON.JSON object; importorg.Apache.com mons.io.fileutils; importorg.Apache.com mons.io.I outils; import org.dom4j.Document; importorg.dom4j.document exception; import org.dom4j.DocumentHelper; import org.dom4j.Element; importorg.dom4j.io.output format; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.xml.sax.SAXException; import java.io.File; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.net.URL; import java.nio.file.Paths;/* * @ authordongxingli * created on 2019/4/319336000 */publicclassjsonxmlutils { privatestaticfinalstringencoding='//由JSON对象转换的xml字符串-格式化带空格的换行符并输出* * @param json JSON对象* @return干净的xml字符串*/publicstaticstringjsontorn try{documentdocument=JSON; /*格式XML */outputformatformat=output format.createprettyprint (; //将缩进设置为4个空格format.setIndent (' ); format.setindentsize(4; xmlwriter writer=new xmlwriter (Format XML,format ); writer.write(document ); }catch(ioexceptione ) { e.printStackTrace ); } return formatXml.toString (; }/* * *转换为JSON对象xml字符串-空白字符* * @param json JSON对象* @return xml字符串* @ throwssaxexception */publicstaticstringjsong JSON对象文档对象* * @param json JSON对象* @return Document对象*/publicstaticdocumentjsontodocument { jsonobject 字符串规则: JSON.keyset () element root=document helper.createelement (for )规则); addJson

ObjectElement(json.getJSONObject(rootKey), root); document.add(root); // root对象只能有一个,多个时只读一个 break; } return document; } /** * 添加JSONArray Element * * @param jsonArray 传入的json数组 * @param nodeName 节点名称 * @param arrRoot 上层节点 */ private static void addJsonArrayElement(JSONArray jsonArray, String nodeName, Element arrRoot) { for (Object aJsonArray : jsonArray) { JSONObject jsonObject = (JSONObject) aJsonArray; Element node = DocumentHelper.createElement(nodeName); // 继续遍历 for (String key : jsonObject.keySet()) { Element element = DocumentHelper.createElement(key); Object child = JSON.toJSON(jsonObject.get(key)); if (child instanceof JSONArray) { // 递归 addJsonArrayElement(jsonObject.getJSONArray(key), key, node); } else if (child instanceof JSONObject) { addJsonObjectElement(jsonObject.getJSONObject(key), element); node.add(element); } else { element.setText(jsonObject.getString(key)); node.add(element); } } arrRoot.add(node); } } /** * 添加JSONObject Element * * @param json JSON对象 * @param root 上层节点 * @return Element对象 */ private static void addJsonObjectElement(JSONObject json, Element root) { for (String key : json.keySet()) { Element node = DocumentHelper.createElement(key); Object child = json.get(key); if (child instanceof JSONObject) { addJsonObjectElement(json.getJSONObject(key), node); root.add(node); } else if (child instanceof JSONArray) { addJsonArrayElement(json.getJSONArray(key), key, root); } else { node.setText(json.getString(key)); root.add(node); } } } /** * XML字符串转JSON对象 * * @param xml xml字符串 * @return JSON对象 * @throws DocumentException */ public static JSONObject xmlToJson(String xml) throws DocumentException { JSONObject json = new JSONObject(); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(xml)); Element root = document.getRootElement(); json.put(root.getName(), elementToJson(root)); return json; } /** * Element对象转JSON对象 * * @param element Element对象 * @return JSON对象 */ public static JSONObject elementToJson(Element element) { JSONObject json = new JSONObject(); for (Object child : element.elements()) { Element e = (Element) child; if (e.elements().isEmpty()) { json.put(e.getName(), e.getText()); } else { json.put(e.getName(), elementToJson(e)); } } return json; } /** * 文件内容转换成字符串 * * @param filePath 文件路径 * @return 内容字符串 * @throws IOException */ public static String fileToString(URL filePath) throws IOException { return IOUtils.toString(filePath, ENCODING); } /** * 文件内容转换成字符串 * * @param filePath 文件路径 * @return 内容字符串 * @throws IOException */ public static String fileToString(String filePath) throws IOException { return IOUtils.toString(Paths.get(filePath).toUri(), ENCODING); } /** * 字符串输出到文件 * * @param str 字符串内容 * @param filePath 文件路径 * @throws IOException */ public static void stringToFile(String str, String filePath) throws IOException { FileUtils.writeStringToFile(Paths.get(filePath).toFile(), str, ENCODING); } /** * 字符串输出到文件 * * @param str 字符串内容 * @param filePath 文件路径 * @throws IOException */ public static void stringToFile(String str, URL filePath) throws IOException { FileUtils.writeStringToFile(new File(filePath.getPath()), str, ENCODING); }}

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。