我有如下的SOAP响应。我想迭代肥皂消息,并希望以JSONArray格式获取listMetadataResponse标记中的数据。这里是我的样本SOAP响应:将SOAP响应转换为JSONArray
<?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>
我想每个每个属性节点和值作为JSON.So键值对的结果节点作为的JSONObject的,在这种情况下,我想要的结果作为JSONArray中有两个结果JSONObject。
我试过这段代码。我正在获取节点名称,但我没有获取节点值。
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);
}
}
}
}
有没有办法可以让所有的值是字符串在他们是整数的情况?@Chinmay耆那教 –
迭代使用的JSONObject键集中的对象,然后使用'Integer.toString(INT)' –
我想过那个,但对我来说这似乎是一大堆,因为我的数据真的很大。无论如何,我想我将不得不努力使用循环。 –