`
sillycat
  • 浏览: 2536006 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

JAVA JSON to XML String in Ordering

 
阅读更多
JAVA JSON to XML String in Ordering

In my friend’s project, he requires to convert JSON to XML.
He is using an open source project called JSON-java which is using HashMap in side the implementation which does not support ordering.
So the output XML ordering will be not the same as in JSON.
Here is the way to make it working. pom.xml in Spring-Boot Release
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
Here is the Jackson to help to keep the ordering.
package com.sillycat.utils;
import java.io.IOException;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONHelper {
public static void main(String[] args) throws JsonProcessingException, IOException {
String jsonInString = "{\"name\" : \"yiyi\", \"book\": \"java\", \"lan\": \"pdf\" }";
ObjectMapper mapper = new ObjectMapper();
JsonNode jackson = mapper.readTree(jsonInString);
System.out.println("jackson:" + jackson);

String xml = XML.toString(new JSONAdapter(jackson));
System.out.println("XML:" + xml);
}
private static class JSONAdapter extends JSONObject {
private JsonNode jackson;
public JSONAdapter(JsonNode jackson) {
this.jackson = jackson;
}
@Override
public Iterator<String> keys() {
return jackson.fieldNames();
}
@Override
public Object opt(String key) {
return get(key);
}
@Override
public Object get(String key) throws JSONException {
JsonNode nested = jackson.get(key);
if (nested.isObject()) {
return new JSONAdapter(nested);
} else if (nested.isTextual()) {
return nested.asText();
} else if (nested.isNumber()) {
return nested.asDouble();
} else if (nested.isBoolean()) {
return nested.asBoolean();
}
return null;
}
}
}
Since the version of the package is controlled by the Spring Boot Parent Project, we may need this to covert the JSONArray to Java ArrayList
package com.sillycat.utils;
import java.util.ArrayList;
import java.util.Collection;
import org.json.JSONArray;
import org.json.JSONException;
public class ArrayUtil {
public static ArrayList<Object> convert(JSONArray jArr) {
ArrayList<Object> list = new ArrayList<Object>();
try {
for (int i = 0, l = jArr.length(); i < l; i++) {
list.add(jArr.get(i));
}
} catch (JSONException e) {
}
return list;
}
public static JSONArray convert(Collection<Object> list) {
return new JSONArray(list);
}
}
But it is not as good as the indexPosition solutions like the CSV Package.
References:
https://stackoverflow.com/questions/17037340/converting-jsonarray-to-arraylist/17037364
https://stackoverflow.com/questions/42787333/json-to-xml-conversion-without-changing-the-sequence
https://github.com/stleary/JSON-java

分享到:
评论

相关推荐

    Simple Ordering System in Java Free Source Code.zip

    以上是对“Simple Ordering System in Java Free Source Code”可能涉及的主要知识点的概述。通过研究这个系统,你可以深入理解Java编程,并了解到如何构建一个实际的业务应用程序。对于初学者来说,这是一个很好的...

    Ordering点餐项目 Java Oracle

    "Ordering点餐项目"是一个基于Java Swing的前端应用程序,结合Oracle数据库作为后端存储,用于实现餐厅点餐系统。这个项目展示了如何将Java GUI技术和关系型数据库管理系统的功能结合起来,为用户提供友好的界面和...

    java加密工具类 DESUtils

    des是java开发中比较常见的加密方式,多用于加密要求不是很严格的地方,其优点为算法速度比非对称加密的快,缺点为加密密钥和解密密钥为同一个。将加密方式封装成一个工具类,便于使用,可指定密钥或使用默认密钥,...

    java Simple Ordering System (Console).zip

    标题 "java Simple Ordering System (Console).zip" 暗示我们正在处理一个基于Java的简单订购系统,该系统是通过控制台(命令行)进行交互的。这个压缩包包含了一系列的文件,其中cart.class和cart.java是Java编译后...

    OPTICS:Ordering Points To Identify the Clustering Structure

    ### OPTICS:Ordering Points To Identify the Clustering Structure #### 概述 集群分析作为数据库挖掘的主要方法之一,在数据集中洞察分布模式方面扮演着至关重要的角色。它不仅可以作为一种独立工具来帮助聚焦...

    dataset_coco.json+dataset_flickr8k.json+dataset_flickr30k.json

    coco.json+dataset_flickr8k.json+dataset_flickr30k.json"表明这是一组用于图像 caption 任务的数据集,其中包含了COCO(Common Objects in Context)、Flickr8k 和 Flickr30k 这三个知名数据集的JSON格式描述文件...

    Time-Clocks-and-the-Ordering-of-Events-in-a-Distributed-System.pdf

    Lamport的这篇论文《Time-Clocks-and-the-Ordering-of-Events-in-a-Distributed-System》提出了一种利用逻辑时钟来解决分布式系统中事件排序问题的方法。论文通过定义分布式系统中事件的部分顺序和总顺序,详细阐述...

    captions_train2014.json

    标题中的"captions_train2014.json"文件是一个数据集,主要用于训练图像识别和自然语言处理模型。在机器学习和人工智能领域,这样的文件通常包含了大量图像的描述数据,用于帮助模型学习理解图像内容并生成相应的...

    基于Java的网上订餐系统设计源码 - online ordering system

    项目包含48个文件,其中包括33个Java文件、7个XML文件、2个Markdown文档,以及用于版本控制和配置的文件。此外,还有1个gitignore文件、1个jar文件、1个Properties文件、1个mvnw文件和1个cmd文件。该项目旨在为用户...

    DWR.xml配置文件说明书(含源码)

    4 Signatures in dwr.xml Signatures部分用于配置Collections中装载对象元素的类型.举个例子来说:下面的java代码没有办法对List集合中的对象进行转换. public class Check { public void setLotteryResults(List ...

    Java Map 按值排序

    Map&lt;String, Integer&gt; sortedByValue = Maps.toMap( map.entrySet().stream(), entry -&gt; entry.getKey(), entry -&gt; entry.getValue(), Ordering.natural().onResultOf(Maps.Entry::getValue).reverse()....

    jQuery json带分页的动态数据表格代码.zip

    例如,你可以设置分页大小(`lengthMenu`),搜索功能(`searching`),排序功能(`ordering`)等。 4. **JSON数据格式**:JSON文件应该是一个数组,其中每个元素都是一个对象,对象的属性与`columns`配置中的键相...

    Java实现-Topological Graphing

    You will need to modify the topological sort algorithm slightly to allow it to come up with a different valid topological ordering the second time, by tweaking one of the id(0) nodes that it selects.

    resnet50_weights_tf_dim_ordering_tf_kernels.h5

    这是两个文件,resnet50_weights_tf_dim_ordering_tf_kernels_h5和resnet50_weights_tf_dim_ordering_tf_kernels_notop,刚好可以压缩到一起上传,其他几个也压缩好上传了,需要的找找

    think in c++

    It was created by Chuck Allison for my company (MindView, Inc.), and it's called "Thinking in C: Foundations for Java and C++." It introduces you to the aspects of C that are necessary for you to ...

    XZ-Ordering_A_Space-Filling Curve.pdf

    XZ-Ordering_A_Space-Filling Curve.pdf XZ-Ordering是一种空间填充曲线,它用于对象具有空间扩展的空间索引结构。传统的关系数据库管理系统无法有效地管理空间信息,地理信息系统(GIS)需要处理大量的空间和主题...

    CPU Cache and Memory Ordering

    – CPU Memory Ordering模型 (Atomic,Reorder,Memory Barrier (Compiler, CPU),Lock Instruction,Load Acquire/Store Release); – 并发程序设计 (实现一个Spinlock,纠正一个Lock-Free Algorithm, Data Race ...

    online-food-ordering-system-源码.rar

    本文将深入探讨“online-food-ordering-system-源码.rar”中包含的源代码,解析其核心功能和架构,以帮助读者理解此类系统的实现原理。 1. 系统架构 在线订餐系统通常基于三层架构:表现层(前端)、业务逻辑层...

    Structures and Properties of NbOF3 and TaOF3 - with a Remark to the O/F Ordering in the SnF4 Type Structure

    Structures and Properties of NbOF3 and TaOF3 - with a Remark to the O/F Ordering in the SnF4 Type Structure Structures and Properties of NbOF3 and TaOF3 � with a Remark to the O/F Ordering in the ...

    Delegated persist ordering 中文翻译

    ### Delegated Persist Ordering 中文翻译 #### 摘要 本文主要介绍并探讨了复杂网络的研究背景及其重要性。作者Steven H. Strogatz,作为美国康奈尔大学理论与应用力学及应用数学教授,从多学科角度出发,深入讨论...

Global site tag (gtag.js) - Google Analytics