首页 > 解决方案 > 在将 JSON 转换为 XML 时添加额外的元数据

问题描述

我想将 JSON 响应转换为基于 SOAP 的 XML 响应(使用underscore-java)。

    import com.github.underscore.lodash.U;

    public class JsonToXml {
        public static void main(String[] args) {
            String json = "[{\"id\":\"1\",\"name\":\"Bratislava\",\"population\":\"432000\"},{\"id\":\"2\",\"name\":\"Budapest\",\"population\":\"1759000\"},{\"id\":\"3\",\"name\":\"Prague\",\"population\":\"1280000\"},{\"id\":\"4\",\"name\":\"Warsaw\",\"population\":\"1748000\"},{\"id\":\"5\",\"name\":\"Los Angeles\",\"population\":\"3971000\"},{\"id\":\"6\",\"name\":\"New York\",\"population\":\"8550000\"},{\"id\":\"7\",\"name\":\"Edinburgh\",\"population\":\"464000\"},{\"id\":\"8\",\"name\":\"Berlin\",\"population\":\"3671000\"}]";

            String jsonToXml = U.jsonToXml(json);

            System.out.println(jsonToXml);
        }
    }

在将 JSON 转换为 XML 时,我们如何添加以下内容以响应 XML?

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <soapenv:Header/>
       <soapenv:Body>

标签: javajsonxmlunderscore-java

解决方案


代码:

    String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
        + "   <soapenv:Header/>"
        + "   <soapenv:Body><Cities><City></City></Cities>"
        + "   <OperationResponse> <Type>SUCCESS</Type> <Code>0</Code> <ResponseDetails> <ResponseDetail> <Message>Operation completed successfully</Message> </ResponseDetail> </ResponseDetails> </OperationResponse>"
        + "   </soapenv:Body>"
        + "</soapenv:Envelope>";
    Map<String, Object> map = (Map<String, Object>) U.fromXml(xml);
    String json = "[{\"id\":\"1\",\"name\":\"Bratislava\",\"population\":\"432000\"},{\"id\":\"2\",\"name\":\"Budapest\",\"population\":\"1759000\"},{\"id\":\"3\",\"name\":\"Prague\",\"population\":\"1280000\"},{\"id\":\"4\",\"name\":\"Warsaw\",\"population\":\"1748000\"},{\"id\":\"5\",\"name\":\"Los Angeles\",\"population\":\"3971000\"},{\"id\":\"6\",\"name\":\"New York\",\"population\":\"8550000\"},{\"id\":\"7\",\"name\":\"Edinburgh\",\"population\":\"464000\"},{\"id\":\"8\",\"name\":\"Berlin\",\"population\":\"3671000\"}]";
    List<Object> list = (List<Object>) U.fromJson(json);
    U.set(map, "soapenv:Envelope.soapenv:Body.Cities.City", list);
    System.out.println(U.toXml(map));

输出:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header/>
  <soapenv:Body>
    <Cities>
      <City>
        <id>1</id>
        <name>Bratislava</name>
        <population>432000</population>
      </City>
      <City>
        <id>2</id>
        <name>Budapest</name>
        <population>1759000</population>
      </City>
      <City>
        <id>3</id>
        <name>Prague</name>
        <population>1280000</population>
      </City>
      <City>
        <id>4</id>
        <name>Warsaw</name>
        <population>1748000</population>
      </City>
      <City>
        <id>5</id>
        <name>Los Angeles</name>
        <population>3971000</population>
      </City>
      <City>
        <id>6</id>
        <name>New York</name>
        <population>8550000</population>
      </City>
      <City>
        <id>7</id>
        <name>Edinburgh</name>
        <population>464000</population>
      </City>
      <City>
        <id>8</id>
        <name>Berlin</name>
        <population>3671000</population>
      </City>
    </Cities>
    <OperationResponse>
      <Type>SUCCESS</Type>
      <Code>0</Code>
      <ResponseDetails>
        <ResponseDetail>
          <Message>Operation completed successfully</Message>
        </ResponseDetail>
      </ResponseDetails>
    </OperationResponse>
  </soapenv:Body>
</soapenv:Envelope>

推荐阅读