java - Xml 到 JSON 单值数组处理

标签 java json xml serialization json-lib

我使用以下代码成功地将我的 XML 转换为 Json。

我遇到了这个问题,我在下面的程序中有两个不同的 XML 字符串。

第一个用户名只有一个值,第二个有两个值。

第一个 xml 生成没有方括号的 json。第二个 xml 生成带有方括号的 json。

我希望它们都有方括号。有没有办法在输出字符串中将用户名强制转换为数组?

首先,我希望它们在输出 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}}}}

提前致谢。

最佳答案

经过大量研究,我找到了处理该问题的最佳方法。

您可以使用下面的代码片段将任何 XML 转换为 JSON 而不会丢失类型信息和数组问题。

我们需要 Java POJO 来像下面这样转换。

通过在下面的代码中使用 AuthInfo.class。我正在获取具有正确数据类型的 JSON。

您可以在此方法中将任何复杂的 XML 转换为 JSON。

package com.java.json;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.jaxb.XmlJaxbAnnotationIntrospector;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import com.tda.trident.beb.AuthInfo;

public class XmlDoc {

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>";
    Object messageObj = null;       

    XmlMapper unmarshaller = new XmlMapper();
    unmarshaller.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    XmlJaxbAnnotationIntrospector xmlIntrospector = new XmlJaxbAnnotationIntrospector();
    unmarshaller.setAnnotationIntrospector(xmlIntrospector);
    JaxbAnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    ObjectMapper marshaller = new ObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    marshaller.setSerializationInclusion(Include.NON_NULL);
    marshaller.setAnnotationIntrospector(introspector);

    TransactionTradeMessage rootNode = unmarshaller.readValue(xmlstring, AuthInfo.class);

    messageObj = rootNode.getTransactionTrade();

    System.out.println(marshaller.writeValueAsString(messageObj));

}
}

关于java - Xml 到 JSON 单值数组处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34053941/

相关文章:

android - android xpath 中的 "TransformerException: A location step was expected"

java - 如何使用Java程序为段落添加标签并动态生成ID?

java - JHipster 页面空白

java - 如何在 web.xml 中为 2 个 url 模式配置不同的过滤器和 Jersey servlet?

javascript - 在 JSON 中搜索对象

java - 将 xml 数据转换为字符串

java - 升级到 JBoss 7 数据源不起作用

java - JAXB 忽略 package-info.java 中的 @XmlAccessorFactory

javascript - 如果 JSON 在 <table> 中返回空显示消息

ios - 如何在 xcode (swift) 中从 Json Schema/Json 生成模型对象?