Kishore
Kishore

Reputation: 2051

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

Upvotes: 0

Views: 2679

Answers (1)

kgiannakakis
kgiannakakis

Reputation: 104188

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,

Upvotes: 1

Related Questions