【发布时间】:2016-03-07 08:31:47
【问题描述】:
我使用以下代码成功地将我的 XML 转换为 Json。
我遇到了这个问题,我在下面的程序中有两个不同的 XML 字符串。
第一个用户名有一个值,第二个有两个值。
第一个 xml 生成没有方括号的 json。第二个 xml 生成方括号。
我希望他们两个都有方括号。有没有办法将用户名强制转换为输出字符串中的数组?
首先,我希望它们看起来像输出 json 中的数组。
package com.java.json;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.xml.XMLSerializer;
public class XmlSerializer {
public static void main(String[] args) throws Exception {
String xmlstring = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header><authInfo xsi:type=\"soap:authentication\" xmlns:soap=\"http://list.com/services/SoapRequestProcessor\"><!--You may enter the following 2 items in any order--><username xsi:type=\"xsd:string\">user@email.com</username><password xsi:type=\"xsd:string\">password</password></authInfo></soapenv:Header></soapenv:Envelope>";
//String xmlstring = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.web.post.list.com\"><soapenv:Header><authInfo xsi:type=\"soap:authentication\" xmlns:soap=\"http://list.com/services/SoapRequestProcessor\"><!--You may enter the following 2 items in any order--><username xsi:type=\"xsd:string\">user@email.com</username><username xsi:type=\"xsd:string\">user@email.com</username><password xsi:type=\"xsd:string\">password</password></authInfo></soapenv:Header></soapenv:Envelope>";
JsonConfig conf = new JsonConfig();
XMLSerializer xs = new XMLSerializer();
xs.setForceTopLevelObject(true);
xs.setSkipWhitespace(true);
xs.setTrimSpaces(true);
xs.setSkipNamespaces(true);
xs.setRemoveNamespacePrefixFromElements(true);
JSONObject jsonobj = (JSONObject) xs.read(xmlstring);
String jsonstring = jsonobj.toString().replace("\"@", "\"");
System.out.println(jsonstring);
}
}
第一个 XML 应该给出这样的输出。
{"Envelope":{"Header":{"authInfo":{"type":"soap:authentication","username":[null],"password":null}}}}
目前是这样给的。
{"Envelope":{"Header":{"authInfo":{"type":"soap:authentication","username":null,"password":null}}}}
上面程序中的第二个XML很好,它给出了这样的输出。
{"Envelope":{"Header":{"authInfo":{"type":"soap:authentication","username":[null,null],"password":null}}}}
提前致谢。
【问题讨论】:
-
你为什么期望单个元素被序列化成一个数组?
-
序列化后我们有一个下游进程,它期望用户名作为数组。如果它不在方括号内,该过程将失败。我们不介意在单值数组的情况下是否可以在序列化之后添加这些大括号。我们只需要把它放在方括号里。我们可以通过解析 json 并在最坏的情况下添加它们来手动进行操作,但我正在尝试一种更好的方法来将用户名的类型定义为输出 json 中的数组,例如将钱转换为数组并添加这些大括号。
-
好的,如果您真的不想手动执行此操作.. 我认为这样的事情可能有效(但我不是 100% 确定)- 构建代表您的 XML/JSON 结构的类并将用户名字段声明为
List<String>。然后使用jackson之类的框架将您的XML解析为此类的实例,然后将此对象序列化为JSON。这可以工作(也许有一些额外的配置)。但恕我直言 - 努力大于收益。
标签: java json xml serialization json-lib