如何从服务器的XML响应中获取JSON?

时间:2012-01-18 11:54:09

标签: android xml json

我的android应用程序发送一个Web服务请求,并从xml中获取Web服务的响应,其中嵌入的数据为JSON。我将其保存在字符串中。现在我不知道如何从这个字符串中获取JSON数据。

    System.setProperty("http.keepAlive", "false");
            // request parameters
            HttpParams params = httpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, 10000);
            HttpConnectionParams.setSoTimeout(params, 15000);
            // set parameter
            HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

            // POST the envelope
            HttpPost httppost = new HttpPost(url);
            // add headers
            httppost.setHeader("SOAPAction", soapAction);
            httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
    //      httppost.setHeader("Content-Length",
    //              String.valueOf(requestEnvelope.length()));
            httppost.setHeader("SOAPAction", "http://tempuri.org/"
                    + methodName);



    //      String responseString = "";
            try {

                // the entity holds the request
                HttpEntity entity = new StringEntity(requestEnvelope);
                httppost.setEntity(entity);
HttpResponse response = httpClient.execute(httppost);
            String result = EntityUtils.toString(response.getEntity());

这是来自服务器的响应,我在结果字符串中得到了它。

<?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>
<ValidatePassCodeResponse xmlns="http://tempuri.org/">
<ValidatePassCodeResult>
[{"ID":1929,"Headline":"Test News","Detail":"","SubmitDate":"1/17/2012 12:08:04 PM"}]
</ValidatePassCodeResult>
</ValidatePassCodeResponse>
</soap:Body>
</soap:Envelope>

提前致谢。

3 个答案:

答案 0 :(得分:2)

这是我删除xml字符串的标签并从中获取json字符串的方法,然后我将字符串解析为json数组并获得结果。

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(new StringReader(result));
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_DOCUMENT) {
                    System.out.println("Start document");
                } else if (eventType == XmlPullParser.START_TAG) {
                    System.out.println("Start tag " + xpp.getName());
                } else if (eventType == XmlPullParser.END_TAG) {
                    System.out.println("End tag " + xpp.getName());
                } else if (eventType == XmlPullParser.TEXT) {
                    responseString = xpp.getText();
                    System.out.println("Text " + xpp.getText());
                }
                eventType = xpp.next();
            }
            System.out.println("End document");

答案 1 :(得分:0)

请查看我的问题并将您的回复字符串放在json viewer中。

How to use Json Parsing?

答案 2 :(得分:0)

我使用XMLPullParser获得了解决方案。 谢谢你的帮助。