简体   繁体   English

如何从xml响应解析json

[英]how to parse json from xml response

How do i parse json by removing the xml tag 我如何通过删除xml标签来解析json

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<soap:Body>

<AddUserResponse xmlns="http://abcd.com/">

<AddUserResult>

{"clsError":{"ErrorCode":110,"ErrorDescription":"Email Already Exist"},"UserID":-1}

</AddUserResult>

</AddUserResponse>
</soap:Body>

</soap:Envelope>

I tried this code in it result is taken as response string which is in the above xml format 我尝试了此代码,结果被作为上述xml格式的响应字符串

String temp = result.substring(282, (length - 62));
System.out.println(temp);
JSONObject object = (JSONObject) new JSONTokener(temp).nextValue();
String query = object.getString("ErrorDescription");

in ddms it says: org.json.JSONException: No value for ErrorDescription 在ddms中说:org.json.JSONException:ErrorDescription没有值

You aren't parsing the json correctly. 您没有正确解析json。 This is correct for reading the ErrorDescription: 这对于阅读ErrorDescription是正确的:

JSONObject object = (JSONObject) new JSONTokener(temp).nextValue();
JSONObject childObject = object.getJSONObject("clsError");        
query = childObject.getString("ErrorDescription");

Also, it isn't appropriate to get the json object by simply getting a substring of the xml. 另外,仅通过获取xml的子字符串来获取json对象也是不合适的。 It will be better to do a regulal xml parsing to retrieve it, 最好进行常规的xml解析来检索它,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM