zu konvertieren, habe ich die SOAP-Antwort wie folgt. Ich möchte über die Soap-Nachricht iterieren und die Daten im listMetadataResponse-Tag im JSONArray-Format abrufen. Hier ist mein Beispiel für eine SOAP-Antwort ist:Um SOAP Antwort zu 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>
ich in JSON.So alle Ergebnisknoten als JSONObject mit jedem Attributknoten und Werte als Schlüsselwertpaar erhalten möchten, in diesem Fall möchte ich das Ergebnis als ein JSONArray mit zwei Ergebnissen JSONObject darin.
Ich habe diesen Code ausprobiert. Ich bekomme Knotennamen, aber ich bekomme die Knotenwerte nicht.
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);
}
}
}
}
Gibt es eine Weise, die ich alle Werte in Fällen als String machen können, wo sie ganze Zahlen sind ?, @Chinmay Jain –
Iterate über das Objekt JSONObject Keyset verwenden, und verwenden Sie dann 'Integer.toString (int)' –
Ich dachte darüber nach, aber das scheint eine ganze Menge für mich zu sein, weil meine Daten wirklich riesig sind. Danke trotzdem, ich denke, ich müsste es mit Loops versuchen. –