如何从服务器的 XML 响应中获取 JSON? [英] How to get JSON from an XML response from server?

查看:43
本文介绍了如何从服务器的 XML 响应中获取 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 System.setProperty("http.keepAlive", "false");//请求参数HttpParams params = httpClient.getParams();HttpConnectionParams.setConnectionTimeout(params, 10000);HttpConnectionParams.setSoTimeout(params, 15000);//设置参数HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);//邮寄信封HttpPost httppost = new HttpPost(url);//添加标题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/"+ 方法名);//字符串 responseString = "";尝试 {//实体持有请求HttpEntity entity = new StringEntity(requestEnvelope);httppost.setEntity(实体);HttpResponse 响应 = httpClient.execute(httppost);字符串结果 = EntityUtils.toString(response.getEntity());

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

<肥皂:身体><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>

提前致谢.

解决方案

我就是这样去掉 xml 字符串的标签并从中获取 json 字符串,然后将字符串解析为 json 数组并获得结果.

>

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();factory.setNamespaceAware(true);XmlPullParser xpp = factory.newPullParser();xpp.setInput(new StringReader(result));int eventType = xpp.getEventType();而(事件类型!= XmlPullParser.END_DOCUMENT){if (eventType == XmlPullParser.START_DOCUMENT) {System.out.println("开始文档");} else if (eventType == XmlPullParser.START_TAG) {System.out.println("起始标签" + xpp.getName());} else if (eventType == XmlPullParser.END_TAG) {System.out.println("结束标签" + xpp.getName());} else if (eventType == XmlPullParser.TEXT) {responseString = xpp.getText();System.out.println("文本" + xpp.getText());}eventType = xpp.next();}System.out.println("结束文档");

My android application sends a web service request and gets the response from web service in xml, with the embedded data in JSON. I am saving this in a string. Now I do not know how to get the JSON data from this string.

    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());

This is the response from the server, which I get in the result string.

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

Thanks in advance.

解决方案

This is how I removed the tags of xml string and obtained the json string from that, then I parsed the string to json array and obtained the result.

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");

这篇关于如何从服务器的 XML 响应中获取 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆