如何从xml响应中解析json

时间:2010-11-29 07:39:36

标签: android xml json soap

如何通过删除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>

我尝试了这段代码,结果被作为上述xml格式的响应字符串

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

在ddms中它说: org.json.JSONException:ErrorDescription没有值

1 个答案:

答案 0 :(得分:1)

您没有正确解析json。这对于读取ErrorDescription是正确的:

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

此外,通过简单地获取xml的子字符串来获取json对象是不合适的。最好做一个有规律的xml解析来检索它,