我正在尝试将 API 的响应(XML 格式)转换为 JSON 字符串。但它抛出以下错误:
org.json.JSONException:第 573 字符处的元和头部不匹配
Java 代码
final String API_URI = "https://221.120.211.69:443/abl-api/uat/ApiConnectFTwsdlDefinitionHttpService";
final ClientConfig config = new DefaultClientConfig();
final Client client = Client.create(config);
final WebResource resource = client.resource(API_URI);
MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
queryParams.add("SystemName", "CDC");
queryParams.add("RequestID", "222");
queryParams.add("TenantID", "abc11323===+");
queryParams.add("CreditAccount", "0010000108290010");
queryParams.add("DebitAmount", "100");
queryParams.add("MappingID", "Remarks");
queryParams.add("Narration", "remarks");
WebResource webResource = client.resource(API_URI);
JSONObject requestObj = new JSONObject();
requestObj.put("SystemName", "CDC");
requestObj.put("RequestID", "222");
requestObj.put("TenantID", "abc11323===+");
requestObj.put("CreditAccount", "0010000108290010");
requestObj.put("DebitAmount", "100");
requestObj.put("MappingID", "Remarks");
requestObj.put("Narration", "remarks");
ClientResponse responseMsg = webResource
.queryParams(queryParams)
.header("Content-Type", "application/json;charset=UTF-8")
.header("SOAPAction", "http://ApiConnectFTwsdlDefinition/FundsTransfer")
.header("X-IBM-Client-Id", "0146b9a4-7e99-4c83-8e9e-6049cfec55da")
.header("Authorization", "Bearer "+ "AAIkMDE0NmI5YTQtN2U5OS00YzgzLThlOWUtNjA0OWNmZWM1NWRh5BUqmbzniU-ky5TE1-NI9I4IwXqdU8_EEzAEe6QVdclRLwQlVxuO03e3xAVKKOc3xstoep8xbkjK5t3ffbCRJR6xlcGdoS3nvGmEoMYgbesChrbhrAnu6uVS8g-JRSGWaqwqTf-DRQRBRi6ysKEOOg")
.post(ClientResponse.class, requestObj);
JSONObject xmlJSONObj = XML.toJSONObject(responseMsg.getEntity(String.class));
String jsonPrettyPrintString = xmlJSONObj.toString(4);
//System.out.println(xmlJSONObj.toString());
System.out.println(jsonPrettyPrintString);
//System.out.println(responseMsg.getEntity(String.class));
我尝试将 XML 更改为字符串和 JSON 实体,并更改了 JSON Pretty 常量,但没有任何改变。
有人可以帮忙吗?
最佳答案
好吧,我将响应从 String 转换为 Hashmap 并使用了 key 。
关于Java - XML 到 JSON 转换(org.json.JSONException : Mismatched meta and head at character 573 ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60182595/