2010-11-29 129 views
0

我如何解析JSON通過刪除XML標記如何從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中

+0

當你輸入你的問題,在右邊有一個框的標題**如何格式**。 Ask a Question文本區域上方還有一個** [?] **鏈接。兩者都值得一讀。這是你的第四個問題,你現在應該得到你的雙腿。 – 2010-11-29 07:43:03

回答

1

沒有價值你沒有正確地解析JSON。這是正確的閱讀ErrorDescription:

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

此外,通過簡單地獲取XML的子字符串來獲取json對象是不合適的。這將是更好的做一個正規的XML解析來檢索它,