简体   繁体   中英

how to parse json from xml response

How do i parse json by removing the xml tag

<?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

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

You aren't parsing the json correctly. This is correct for reading the 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. It will be better to do a regulal xml parsing to retrieve it,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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