将SOAP响应转换为JSONArray [英] To convert SOAP response to JSONArray
问题描述
我有如下的SOAP响应。我想迭代soap消息,并希望以JSONArray格式获取listMetadataResponse标记中的数据。这是我的示例SOAP响应:
I have the SOAP response as below. I want to iterate over the soap message and want to get the data in the listMetadataResponse tag in JSONArray format. Here is my sample SOAP response:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata">
<soapenv:Body>
<listMetadataResponse>
<result>
<createdById>00528000001m5RRAAY</createdById>
<createdByName>Hariprasath Thanarajah</createdByName>
<createdDate>1970-01-01T00:00:00.000Z</createdDate>
<fileName>objects/EmailMessage.object</fileName>
<fullName>EmailMessage</fullName>
<id />
<lastModifiedById>00528000001m5RRAAY</lastModifiedById>
<lastModifiedByName>Hariprasath Thanarajah</lastModifiedByName>
<lastModifiedDate>1970-01-01T00:00:00.000Z</lastModifiedDate>
<namespacePrefix />
<type>CustomObject</type>
</result>
<result>
<createdById>00528000001m5RRAAY</createdById>
<createdByName>Hariprasath Thanarajah</createdByName>
<createdDate>1970-01-01T00:00:00.000Z</createdDate>
<fileName>objects/EmailMessage.object</fileName>
<fullName>EmailMessage</fullName>
<id />
<lastModifiedById>00528000001m5RRAAY</lastModifiedById>
<lastModifiedByName>Hariprasath Thanarajah</lastModifiedByName>
<lastModifiedDate>1970-01-01T00:00:00.000Z</lastModifiedDate>
<namespacePrefix />
<type>CustomObject</type>
</result>
</listMetadataResponse>
</soapenv:Body>
</soapenv:Envelope>
我想将每个结果节点作为JSONObject获取,每个属性节点和值作为键值对在JSON.So中,在这种情况下,我希望结果为JSONArray,其中包含两个结果JSONObject。
I want to get each of the result nodes as JSONObject with each attribute node and values as key value pair in JSON.So, in this case, I want the result as a JSONArray with two results JSONObject in it.
我试过这段代码。我正在获取节点名称,但我没有得到节点值。
I have tried this code. I am getting node names but I am not getting the node values.
private static Document loadXMLString(String response) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(response));
return db.parse(is);
}
public static JSONArray getFullData(String tagName, String request) throws Exception {
JSONArray resultArray = new JSONArray();
Document xmlDoc = loadXMLString(request);
NodeList nodeList = xmlDoc.getElementsByTagName("*");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equals("result")) {
JSONObject rootObject = new JSONObject();
NodeList childNodeList = nodeList.item(i).getChildNodes();
for (int j = 0; j < childNodeList.getLength(); j++) {
node = childNodeList.item(i);
rootObject.put(node.getNodeName(), node.getNodeValue());
}
resultArray.put(rootObject);
}
}
}
}
推荐答案
您可以通过stleary使用JSON-java库。
You can use JSON-java library by stleary.
您可以使用以下代码将XML字符串转换为JSONObject。
You can use the following code to convert an XML string into JSONObject.
JSONObject data = XML.toJSONObject(xmlString);
您可以在此处找到有关它的更多信息: JSON-java
You can find more info about it here: JSON-java
这篇关于将SOAP响应转换为JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!